Something wierd is happening in my javascript and i dont understand it. Can anyone explain?
var adsl2pSpeed = '9500 - 12500';
alert(adsl2pSpeed);
if (!adsl2pSpeed) {
alert(adsl2pSpeed);
var adsl2pSpeed = 'Unknown';
}
var speed = document.getElementById("PredictedSpeed");
speed.innerHTML = adsl2pSpeed + " b/s";
This alerts "Undefined" twice and sets the innerhtml to be "Unknown". If I comment out the if statment it alerts '9500 - 12500' and sets the innerHTML to be '9500 - 12500'. Whats happening? Is the string is being cast as an object so it becomes null?
EDIT : I am actually registing the adsl2pSpeed as a startup script not in the function. I moved it up for clarity but possibly that is the problem?
varbeforevar adsl2pSpeed = 'Unknown';... the variable adsl2pSpeed has already been declared.varis causing the problem because the indented code is in its own function (which Tom did not tell us about), and thevarmakes a new local variable.