Results 1 to 3 of 3

Thread: Onload

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Onload

    Is there a way to give tags that do not support the onload attribute (such as div) a working onload attribute? Or would I have to mimic it by placing a script tag right after the desired tag?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You would probably have to do something like this:

    Code:
    <div id="test">Some text</div>
    
    <script type="text/javascript">
    window.onload = document.getElementById('test').innerHTML = 'This is a test';
    </script>
    Not sure if this is the best way, but hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Is there a way to give tags that do not support the onload attribute (such as div) a working onload attribute? Or would I have to mimic it by placing a script tag right after the desired tag?
    You can poll it:
    Code:
    <script type="text/javascript">
      function onExist(elID, func) {
        var v = document.getElementById(elID);
        if(!v)
          setTimeout(function() { onExist(elID, func); }, 500);
        else
          func.call(v);
        v = null;
      }
    
      onExist("test", function() {
        alert(this.firstChild.nodeName);
      });
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •