静态脚本标记中的跨浏览器 onload 事件
是否有跨浏览器的方法将 onload 事件关联到 html 文档中的静态脚本标记?
以下内容在 IE 7 和 IE 8 中不起作用:
<script onload="DoThat" type="text/javascript" src="..."></script>
一些背景
我找到了一种使用动态脚本标记和 if 语句来完成此操作的方法。例如,这篇 MSDN 文章< /a>.
我的问题是我需要找到当前的脚本标签,因为我正在构建就地插入 DOM 元素的小部件。过去我找到了一些解决方法来做到这一点,但它们都有其缺点。我希望在脚本加载事件上使用“this”关键字会有帮助。
Is there a cross-browser way to associate an onload event to a static script tag, in a html document?
The following will not work in IE 7 and IE 8:
<script onload="DoThat" type="text/javascript" src="..."></script>
Some background
I have found a way to accomplish this with a dynamic script tag and if statements. It is for example explained in this MSDN article.
My issue is that I need to locate the current script tag, because I am building widgets that insert DOM elements in-place. In the past I have found some workarounds to do this, but they all have their downside. I am hoping that using the "this" keyword on a script onload event will help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,您需要代码才能知道它属于哪个脚本元素。然后,您可以在该脚本旁边插入必要的 HTML 小部件内容或附加到父脚本等。如果是这样,那么我会向您建议另一种方法。
在您的小部件 JavaScript 文件中,放置下一行代码:
thisScript
是您要查找的 DOM 元素,此代码位于的当前脚本元素。就这么简单。为什么它有效。
详细说明http://feather.elektrum.org/book/src.html。只是一个摘要:
这是相当简单且有效的技术,尽管但没有多少人知道这种可能性。它可靠且 100% 浏览器兼容。顺便说一下,Google 使用这段代码来渲染 +1 按钮:
那么,你认为 api 脚本如何获取这个哈希参数呢?在我的示例中,它将是
thisScript.innerHTML
As far as I understand, you need your code to know what script element it belongs to. You would then insert necessary HTML widget content right next to this script or append to the script parent, etc. If so then I would propose you another approach.
Inside you widget javascript file you place next line of code:
thisScript
is the DOM element you are looking for, current script element this code located in. So simple.Why it works.
Detail explanation http://feather.elektrum.org/book/src.html. Just an abstract:
This is fairly simple and effective technic, although but not so many people know about this possibility. It's reliable and 100% browser compatible. By the way, Google uses this code to render +1 buttons:
So how do think how the api script could get this hash parameters? In my example it would be
thisScript.innerHTML
我不确定我是否满足了您的需求。
无论如何要找到当前脚本,您可以尝试这个示例
I'm not sure if I caught your needs.
Anyway to locate the current script you can try this example