I have two javascripts that need to be initiated with onload. Looking at the w3c school information I can't find informaton on how to do this. I tried onload= "func1, func2" and "func1", "func2" without success. Can this be done and what is the correct syntax?
That does not do what you think that it does. As the piece of script is parsed, id est long before the onload event is triggered, loadMe will be called and whatever is returned will be assigned to the onload handler. In this case undefined is returned (or possible null, my JavaScript theory has become a bit rusty since I was banned from that forum) so on load the browser will try to do undefined (or do null). It'll either do nothing or throw an error. The function loadMe will be called, but it will be called before the page is loaded. You want:
window.onload = function () {
function_a()
function_b()
}
Or even better
HTML Code:
<body onload="function_a(); function_b()">
Last edited by Charles; 08-10-2011 at 06:29 PM.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Whoops, my mistake on putting a space in 'load me();' - it will work correctly how I showed - bar the space!
But what Charles quoted, is the one you're looking for:
HTML Code:
<body onload="function_a(); function_b()">
The space is not the problem. The problem is the "()". That calls the function and then what is returned is assigned. You want to assign the function itself to the handler. In JavaScript you're really creating methods not functions and methods are really properties and can be assigned. The function is called either way, it's just a question of when and what error is thrown.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Bookmarks