well I am analyzing a javascript code, but I got confuse in some lines, for example I have this code
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
if (y.substr(0, 1) == "#"){ return y; } else {
var value = y.split(',');
var R = value[0].substr(4);
var G = value[1];
var B = value[2].substr(0, value[2].length-1);
var RGB = "#" + toHex(R)+ toHex(G)+toHex(B);
return RGB;
}
}
function toHex(N) {
if (N==null) return "00";
N=parseInt(N); if (N==0 || isNaN(N)) return "00";
N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
return "0123456789ABCDEF".charAt((N-N%16)/16)
+ "0123456789ABCDEF".charAt(N%16);
}
function pw (form)
{
var d1, d2, d3;
if (navigator.appName == "Netscape"){
d1= getStyle('content', 'background-color');
} else {
d1= getStyle('content', 'backgroundColor');
}
d2=form.Name.value;
d3=form.Name2.value;
Firs of all, I dont know what the variables “R”, “G” and “B” are doing?, are they affecting the variable “d1”? I know that variables “d2” and “d3” are the values of what they said, but what is the value of the variable “d1”?
Some help will be appreciated. Tnx.
This code is pretty straight forward. Variables R, G and B each contain a color, R – red, G – green and B – blue. This only happens if the style doesn’t already represent the color through in a hex format. They are there to be able to convert color back to hex and return in. So yes, in a way they do affect the variable d1. If background-color style is going to be set like this: background-color: #FF00FF d1 is going to be #FF00FF. If the background color is going to be set like this: background-color: rgb(00, 255, 00) d1 is going to be #0000FF, since this function expects some none-existing format like this: background-color: 00, 255, 00 (this is not valid CSS).
In general this seems like a poorly named and written code.