最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论
<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>
发布评论

评论列表(0)

  1. 暂无评论