if (window.addEventListener){ window.addEventListener("load",init,false); document.addEventListener("mousemove",mouse,false); } else if (window.attachEvent){ window.attachEvent("onload",init); document.attachEvent("onmousemove",mouse); }
This should generally suffice:
PHP Code:
window.onload = init;
Or this, if you need to pass parameters to init() or perform other actions as well:
PHP Code:
window.onload = function() { init(); }
Though, the simplest solution, in my opinion, is to place your "onload" code or method call at the bottom of the page, immediately before the closing body tag. You don't necessarily have the assurance that all images are loaded at that point, but all DOM nodes and preceding scripts will be.
window.onload = function(){
... a bunch of stuff ...
window.onload = init;
}()
My best guess offhand as to what's happening:
The function() being set to window.init is being run as an anonymous function, the result of which (there is no result in this case) is being assigned to window.onload, which is replacing the assignment within the function (window.onload = init).
To take a simple example, the following line will define x as a function which alerts a literal x:
PHP Code:
var x = function() { alert('x'); }
This line, on the other hand, will execute an anonymous function, alert a literal ximmediately, and then dump the return value (null) into x after the alert box is cleared:
PHP Code:
var x = function() { alert('x'); }()
First thing to try, get rid of the firstwindow.onload =. Leave the window.onload = init; line in place. See what happens.
window.onload = function(){
... a bunch of stuff ...
window.onload = init;
}()
My best guess offhand as to what's happening:
The function() being set to window.init is being run as an anonymous function, the result of which (there is no result in this case) is being assigned to window.onload, which is replacing the assignment within the function (window.onload = init).
To take a simple example, the following line will define x as a function which alerts a literal x:
PHP Code:
var x = function() { alert('x'); }
This line, on the other hand, will execute an anonymous function, alert a literal ximmediately, and then dump the return value (null) into x after the alert box is cleared:
PHP Code:
var x = function() { alert('x'); }()
First thing to try, get rid of the firstwindow.onload =. Leave the window.onload = init; line in place. See what happens.
Yup. And at this point, I think it's best that I instruct you to use your browser's debug console.
If you're using Chrome, you can right-click on the page, click Inspect Clement, and select the Console tab.
If you're using Internet Explorer, press F12 and select the Console or Script tab.
If you're using Firefox, find the Error Console from the Web Developer menu or press Ctrl-Shift-J.
The error I'm seeing is exceptionally obvious if you take a brief moment to look for it!
Yup. And at this point, I think it's best that I instruct you to use your browser's debug console.
If you're using Chrome, you can right-click on the page, click Inspect Clement, and select the Console tab.
If you're using Internet Explorer, press F12 and select the Console or Script tab.
If you're using Firefox, find the Error Console from the Web Developer menu or press Ctrl-Shift-J.
The error I'm seeing is exceptionally obvious if you take a brief moment to look for it!
Bookmarks