...

View Full Version : window.onload loads before content



FlipperBizkut
08-31-2006, 12:03 AM
I am a total noob to javascript. What I am trying to do is to change links dynamically if javascript can be run on the user's computer. The code that I have come up with to do this is:



function changeLinks(eleId) {
if (document.getElementById) {
var textLink = document.getElementById(eleId);
if (textLink.getAttribute('href') == "#") {
textLink.setAttribute('href', "javascript:showdiv('hide');");
}
}
}

window.onload = changeLinks('show');


This is all fine and good except that I keep getting an error of "textLink has no properties" and the script doesn't work.

If I get rid of the "window.onload" event and call the function in script tags at the bottom of my page, it works fine. Just not when I try to have it do it automatically when the page loads via this external script.

Just for the record, the code above is the entire external javascript file, and I am calling it in my HTML with this line:



<script type="text/javascript" src="javascript.js"></script>


I don't have a clue as to what I am doing wrong. It seemed to me like it should work.

Any help?

liorean
08-31-2006, 12:08 AM
The problem lies in this line:

window.onload = changeLinks('show');
You're setting window.onload to the return value of calling the function changeLinks with the argument 'show'.

In other words, you're doing the function call first, and then adding the result from it as the load even handler. Try the following instead:

window.onload=function(){changeLinks('show')};

FlipperBizkut
08-31-2006, 12:16 AM
That did it. Wow. I would have never figured that out!

Thanks a lot!

By the way, can you recommed any books for the absolute noob on javascript? I went out and got "Beginning Javascript with DOM Scripting and AJAX", but it is really hard to follow. I wouldn't say that it is for the beginner at all!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum