admin管理员组文章数量:1415139
<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?
3 Answers
Reset to default 3There 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 variableoB
.
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>
本文标签: javascriptHow to listen to a script tag39s onload eventStack Overflow
版权声明:本文标题:javascript - How to listen to a script tag's onload event? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745228551a2648720.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论