The code that i am working on is this-
var winW;
if (document.body && document.body.offsetWidth) {
winW = document.body.offsetWidth;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth ) {
winW = document.documentElement.offsetWidth;
}
if (window.innerWidth && window.innerHeight) {
winW = window.innerWidth;
}
if (winW = 1265) {
function setPosition()
{
document.getElementById("wblogo").style.left="1050px";
}
}
The code works perfectly when i use it with the onload event in the body tag.
But i want to use this when the page is being loaded. So i used the following code-
var winW;
if (document.body && document.body.offsetWidth) {
winW = document.body.offsetWidth;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth ) {
winW = document.documentElement.offsetWidth;
}
if (window.innerWidth && window.innerHeight) {
winW = window.innerWidth;
}
if (winW = 1265) {
document.getElementById("wblogo").style.left="1050px";
}
But this code doesn’t work. Please help.
My guess is that when you removed the
onload, you left your script in theheadsection of the document. The elements you’re trying to work with (document.body, yourwblogoelement) don’t exist yet, so the code can’t interact with them. When you were usingonload, your code wasn’t being run right away, but instead very late in the page load cycle after all images and such were loaded, and so the elements did exist.Instead, put your code in a
scripttag at the end of your document, just before the closing</body>tag. That way, the elements you’re trying to work with will exist.More: