SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onload event for script element (internet explorer)

    hello,

    <script src='xxx.js' onload='alert("hello")'></script>

    It isn't working in internet explorer, how can i fix it?

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you really, really need to do that? If you want to ensure all scripts have loaded, you'd be better off using window.onload.

    If you absolutely must do it, then forget the onload and just have alert("hello"); (or whatever) at the end of your xxx.js script.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script type="text/javascript">
    onload= function(){alert("hello")}
    </script>

  4. #4
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you really, really need to do that?
    Yes, i use "script" element in my ajax works and i need an "onComplete" property for this element.

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In IE it looks like onload is broken for the <script> tag.

    Maybe try:
    onreadystatechange="if(this.readyState=='complete')alert('hi');"

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only cross-browser solution for is what ceeb suggested. Take it from there. Maybe have a function that the loaded external scripts notify or whatever.

  7. #7
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by civciv View Post
    Yes, i use "script" element in my ajax works and i need an "onComplete" property for this element.
    OK, since this is for AJAX, I assume you're loading the script by adding a new <script> tag to the DOM in the HTML <head>? I guess that you're also sending data to that script to fetch some results? e.g.

    Code:
    <script type="text/javascript" src="ajaxcall.php?data1=1&data2=2"></script>
    If so, then you could append a callback argument to this data which contains the name of the function you need to run once the script has loaded, e.g.

    ajaxcall.php?data1=1&data2=2&callback=runme

    Your server side script would then put that call at the end of the code, e.g.

    Code:
    <?php
    header('Content-type: text/javascript');
    
    // ... do work and output JS code ... 
    
    // run callback function
    $callback = $_GET['callback'];
    if ($callback != '') echo "$callback()\n";
    ?>


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
  •