Onload event for script element (internet explorer)


<script src=‘xxx.js’ onload=‘alert(“hello”)’></script>

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

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.

<script type=“text/javascript”>
onload= function(){alert(“hello”)}

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.

In IE it looks like onload is broken for the <script> tag.

Maybe try:

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.

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.

<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.


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

header('Content-type: text/javascript');

// ... do work and output JS code ...

// run callback function
$callback = $_GET['callback'];
if ($callback != '') echo "$callback()\