javascript - How to listen to a script tag's onload event? - Stack Overflow

<script id="me" src=".11.3jquery.min.js"><script<script>var oB =

<script id="me" src=".11.3/jquery.min.js"></script
<script>
    var oB = document.getElementsById('me');
    me.onload = function(){
        alert('OK');
    }
</script>

Why me.onload is not triggered after the script is loaded?

<script id="me" src="https://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script
<script>
    var oB = document.getElementsById('me');
    me.onload = function(){
        alert('OK');
    }
</script>

Why me.onload is not triggered after the script is loaded?

Share Improve this question edited May 25, 2015 at 9:42 avetisk 12.3k4 gold badges28 silver badges38 bronze badges asked May 22, 2015 at 9:35 user4329409user4329409 231 gold badge1 silver badge6 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

There are 2 issues:

  • a missing > at the end of the first line (you have written </script instead of </script>)
  • there is no variable me: you have retrieved the script tag into a variable oB.

Thus, you can fix your code by change me.onload = ... to ob.onload = ....

Moreoever, you should avoid using inlined declaration of event listeners such as <script onload="...">.

Last but not least, you should use addEventListener instead of onxxx: addEventListener vs onclick

document.getElementById instead of document.getElementsById

oB instead of me

<script id="me" src="https://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script
<script>
    var oB=document.getElementById('me');
    oB.onload=function(){
        alert('OK')
    }
</script>

But this won't work either because me is already loaded like the other answer states.

before you execute the alert codes, the script tag with id "me" is already downloaded, so despite the syntax error in your code, you can not get the alert.

you can simply use:

<script id="me" onload="alert('OK');"src="https://ajax.googleapis./ajax/libs/jquery/1.11.3/jquery.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745228552a4617574.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信