Try this in IE and in FF, just to see the difference:
Code:
alert(typeof window.innerWidth)
Anyways, the innerWidth and innerHeight properties of the window object are not supported in IE. In modern versions of IE and with the DOCTYPE that you are using, these expressions will get you an equivalent value:
document.documentElement.clientWidth
and:
document.documentElement.clientHeight
In modern FF, these properties can be used but, in Opera and in older Mozilla based browsers, they will not generate the desired values. Also, in older IE versions, the document.body, not the document.documentElement must be used.
Oddly, your script should work in Opera 9.01 as is. However, it doesn't. The button does move but, you cannot see this until you bring another window into focus and then return to the Opera browser window. I am not sure what causes this odd behavior.
Without solving that last bit, at least not yet, here is a rewrite of your function buttonMove() that should be compatible with most browsers:
Code:
function buttonMove() {
var truebody=document.compatMode && document.compatMode!="BackCompat"? document.documentElement : document.body;
window.innerWidth=window.innerWidth? window.innerWidth : truebody.clientWidth;
window.innerHeight=window.innerHeight? window.innerHeight : truebody.clientHeight;
//get the button
var button = document.getElementById("button");
//generate random number to use for left style
if (l <= (window.innerWidth / 2))
l = (rand((window.innerWidth / 2), (window.innerWidth)-75));
else
l = (rand(5, (window.innerWidth / 2)));
//apply left style
button.style.left = l + "px";
//generate random number to use for top style
if (t <= (window.innerHeight / 2))
t = (rand((window.innerHeight / 2), (window.innerHeight)-25));
else
t = (rand(50, (window.innerHeight / 2)));
//apply top style
button.style.top = t + "px";
}
Ahh, the problem in Opera appears to be just a rendering bug, if an actual button element is used, the effect works seamlessly:
HTML Code:
<button onmouseover="buttonMove();" class="btnmove" id="button"
name="mover">Click Me!</button>
This does not affect modern IE or FF but, the button element was unsupported at some point in the past for Mozilla based and other browsers.
Bookmarks