views:

367

answers:

1

Hi guys! I'm wondering how to use "addEventListener" resp. "attachEvent" correctly!?

window.onload=function(myFunc1){ // do something }

function myFunc2(){ // do something }
if (window.addEventListener){ window.addEventListener('load', myFunc2, false); } 
else if (window.attachEvent){ window.attachEvent('onload', myFunc2); }

...

or

function myFunc1(){ // do something }
if (window.addEventListener){ window.addEventListener('load', myFunc1, false); } 
else if (window.attachEvent){ window.attachEvent('onload', myFunc1); }

function myFunc2(){ // do something }
if (window.addEventListener){ window.addEventListener('load', myFunc2, false); } 
else if (window.attachEvent){ window.attachEvent('onload', myFunc2); }

...

?

Is this cross-browser secure or should I better go with sth. like this:

function myFunc1(){ // do something }
function myFunc2(){ // do something }
...

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

addOnloadEvent(myFunc1);
addOnloadEvent(myFunc2);
...

AND: Say "myfunc2" is for IE 7 only. How to modify the correct/preferred method accordingly?

Thank you so much!

A: 

As for your question: your first method seems weird. The second and the third are both probably OK.

However a more general remark is probably appropriate: the only reasonable cross-browser secure way of doing this stuff (as well as more-or-less any JS stuff in the browser) is by using a javascript framework such as jQuery, Dojo, MochiKit, and the like. So, unless you are developing a framework of your own, DO NOT try to do whatever you are trying to do now.

Many people spent uncountable sleepless nights fighting these issues so that you could now code JS comfortably.

AND: There are zillions of ways of detecting the browser and those again depend on what you need. If you really only need to detect IE7 (who'd want that??), you might try simply checking navigator.appVersion.indexOf("MSIE 7.")

KT
I see... But what to do then if external linking to the e.g. jQuery framework is not possible due to third-party restrictions?
ginny
Thanks! IE7 just as an example. I'm looking for sth. equal to Conditional Comments for JS.
ginny
It all depends on what kind of code quality / effort tradeoffs you are interested in. As I said, I guess that here you might be fairly well off with your second or third methods (the third one is probably an overkill). But in general, if you choose this path, you will soon find yourself fighting a futile battle with the browsers and even stackoverflow won't save you. Do consider any means of including a framework js (or maybe a part of it?) in your project.BTW as a last resort, you can always take a look at how "they" do the stuff.
KT
I'll think about it and have a look for sure. For now, thanks a lot, KT. Bye.
ginny
I couldn't disagree more. Using a "framework" is most certainly not the only reasonable way of doing this, and please don't discourage people from trying to learn how this stuff works. And this particular task isn't actually that hard. Also, using `navigator.appVersion` to test the browser's capabilities is not a good idea: it's much better to test the actual feature you want.
Tim Down
You have your right to disagree (although I have a feeling that you cherry-picked one sentence, took it out of context, and deliberately ignored whatever else I had to say). My experience shows that whenever you decide to solve cross-browser issues on your own, for nearly any but the simplest JS projects, you'll soon end up with what essentially is a framework of your own, with a lot effort spent on "learning" and reinventing the wheel. I presume this was not what the author was going for. Neither did I claim that testing appVersion is a good idea. You're welcome post your own separate answer.
KT
Regarding the suggestion I was cherry-picking, it was the emphasis that drew me to that particular sentence, as it would any reader. I also disagree that I was cherry-picking, seeing as the first paragraph (which the next paragraph dismisses) is the only bit I didn't comment on. I take your point about ending up writing your own framework, but going through that process has advantages: it will improve your JavaScript/DOM skills, will give you greater understanding of what the frameworks do and at what cost, and will give you tools you understand and can easily customize to each project.
Tim Down
I remain at the opinion that the emphasis is completely right, assuming the author is __not__ writing his own js framework - a remark you ignored.It is cool to improve skills, but the question was not about "how do I improve skills", but "how do I solve a problem". Pragmatically, it's more important nowadays to learn a framework rather than delve into the irrelevant issues of "what are the ugly quirks of browser X". Analogy: it's cool to know assembler and quirks of every device, but you'd better go for C and standard drivers. Anyway, I think we made our points clear, thanks for discussion!
KT