admin管理员组文章数量:1426359
I have a javascript that executes within a element like : <a href="javascript:doSomething();">
but it doesn't execute on window.load , anyone knows why does that happen ?
I have a javascript that executes within a element like : <a href="javascript:doSomething();">
but it doesn't execute on window.load , anyone knows why does that happen ?
- because you didnt execute the javascript on window.load and used it on a.click instead maybe. – Numenor Commented Dec 29, 2009 at 14:22
- did you mean window.onload instead of window.load? – Eran Medan Commented Dec 29, 2009 at 14:26
4 Answers
Reset to default 6With the example above doSomething() will only be called when the user clicks on the anchor. If you want it to execute on window.load you need to put the code in the head. i.e.
<script type="text/javascript">
window.onload = doSomething;
</script>
Also, if you're going to be using the onload event a lot I would personally remend getting jQuery and using it's 'on DOM ready' event. This way your javascript will appear seamless to the end-user and won't have a flickering effect.
Have you tried writing <body onload="doSomething();">
?
Assuming you did all the other suggestions, there is always a chance that someone later in the code (after your either <body onload=...
or window.onload = ...
) did exactly the same, and has overriden you.
If it happens to be the case (low chance) the solution to support both of your onload hooks, is window.attachEvent("onload", doSomething)
If I understand your problem correctly, you want to set an attribute in an element, calculating it dynamically via javascript, so that when the page is loaded your link points to the return value of "doSomething()".
For that you can use javascript's DOM manipulation utility functions. In your case something like:
<a id="myLink">my anchor</a>
...
<script type="text/javascript">
getElementById('myLink').href = doSomething();
</script>
本文标签: javascript doesn39t execute on windowloadStack Overflow
版权声明:本文标题:javascript doesn't execute on window.load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469915a2659703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论