SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    crooked window.onload test

    Code:
    <script type='text/javascript'>
    window.onload=function(dd,ddd){alert('one')}
    </script>
    </head>
    <body onload="alert(window.onload)">
    </body>
    how can I test if window.onlaod has been called or not, I have tried with

    window.onload instanceof function
    window.onload.arguments.length

    and many other things but in both cases window.onload existing or not, the returned is the same ...

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code re-defines the onload event twice, which doesn't quite work.

    your best bet is to leave it out of the body tag and just leave it in the javascript.

    If you define:
    Code:
    window.onload = function () {};
    It will run when the browser is done loading the page.

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think typeof window.onload == 'function' is what you're looking for. For example:
    Code:
    function xAddLoadListener(f)
    {
      var t='onload', w=window;
      if(w.addEventListener)w.addEventListener('load',f,false);
      else if(w.attachEvent)w.attachEvent(t,f);
      else {
        var o=w[t];
        w[t]=typeof o=='function' ? function(){o();f();} : f;
      }
    }
    Also, to see how your browser handles different load events (window.onload, document.onload, <body onload="">, etc.), try this test page.

  4. #4
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The aim is to find out if window.onload holds a function or is empty ...

    Code:
    <script type='text/javascript'>
    //window.onload=function(){alert('one')}
    function checkit(){
    alert(typeof window.onload == 'function')}
    </script>
    </head>
    <body onload="checkit()" >
    typeof window.onload return function even if it is "empy" ...

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by "empty"?

    Take the onload attr out of the body tag and then try it.

  6. #6
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to know if window.onload triggers any kind of instruction ...

    so that in this case :
    Code:
    <script type='text/javascript'>
    window.onload=function(){alert('one')}
    function checkit(){
    alert(typeof window.onload == 'function')}
    </script>
    </head>
    <body onload="checkit()" >
    it should tell me that there is a function launched in the onload
    and in this case
    Code:
    <script type='text/javascript'>
    function checkit(){
    alert(typeof window.onload == 'function')}
    </script>
    </head>
    <body onload="checkit()" >
    it would tell me that nothing has been launched ...

  7. #7
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By putting in an onload command in the <body> tag, you're basically turning "onload" into a function ?

    I'm not sure what you're trying to achieve, but here's a good article about unobtrusive javascripting that you may be interested in:

    http://www.onlinetools.org/articles/.../chapter4.html

  8. #8
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jtrelfa is right. When you assign a string to an event handler attribute, for example: <span onclick='foo()'>, it is equivalent to doing this: span.onclick = new Function("foo()");.

    Please refer to my onload test page.

    Notice that, in FF and IE, the assignment to window.onload is over-ridden by the assignment to the body's onload attribute. It is interesting to note that Opera 9 does not do this (it fires both events!) - possibly because it doesn't consider "body" to be equivalent with "window" - which is logical, but however is a long-standing DOM0 assumption.

    Now look at the "Event-Handler Attribute Test" at the bottom of that page. This illustrates what I'm talking about. The assignment to the span.onclick 'property' occurs in the onload code which runs "after" the body is parsed so we expect the property assignment to over-ride the attribute assignment - and it does. The "window.onload" property and its corresponding body attribute also work in the same way (except in Opera) - but an assignment to 'window.onload' usually occurs "before" the body is parsed, so the attribute assignment over-rides the property assignment.

    SpaceFrog, the two code fragments you've posted are identical... aren't they? Please refer to my onload test page. It performs the same type of tests that you are doing there.

  9. #9
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes sorry for identical code .. the second should not have window.onload line ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •