javascript - External JS function onload colliding with user functions onload -


when need fire several functions when page loads, can handle using this:

function myfunction() {     alert('my function!'); }  function pctaddloadevent(func) {   var oldonload = window.onload;   if (typeof window.onload != 'function') {     window.onload = func;   } else {     window.onload = function() {       if (oldonload) {         oldonload();       }       func();     }   } }  pctaddloadevent(myfunction); 

http://jsfiddle.net/ssehr/

this works ok if fire function need fire using pctaddloadevent function. but, if piece of code intended inserted on different pages using external js file need keep in mind robustness of code, , lets include theexamplecode.js in web site, have javascript on web page, , have like:

window.onload = function() {     alert('user function');     //and other user functions } 

that break code can see here.

so in few words, how can start function onload without concerning eventual user functions?

ps. cant use jquery.

i think issue solved using namespacing of functions try out javascript namespacing


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -