需要使用body onload事件,但是第三方JavaScript库已经劫持了它
我正在使用 Simile 来绘制动态时间线。我还使用内部库来添加评论博客。我们的内部库使用 body 元素的 onload 事件进行初始化。
<body onload="initComments('myID')">
但 Simile 似乎出于自己的目的劫持了 onload,因此 initComments('myID')
永远不会执行。
如果不更改 Simile 代码,我怎样才能让我的初始化程序运行?
我不想只是为了解决问题而添加另一个库(即 jQuery)。
I'm using Simile to draw dynamic timelines. I am also using an in-house library to add a comment blog. Our in-house library uses the body element's onload event to initialize.
<body onload="initComments('myID')">
But Simile seems to have hijacked the onload for their own purpose so that initComments('myID')
never executes.
Short of changing the Simile code, how could I get my initializer to run?
I would prefer not to add another library (i.e. jQuery) just to solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,作为一般规则,您希望避免将 JavaScript 作为标记属性嵌入到 HTML 中。
其次,Simile 可能会覆盖
onload
(使用它的方式与您相同)。因此,在包含 Simile 后,您需要添加 onload 事件。使用 jQuery:
不使用库(在此处找到):
First off, as a general rule, you want to stay away from embedding JavaScript in your HTML as tag attributes.
Secondly, Simile is likely overwriting the
onload
(using it the same way you are). So, you'll want to add your onload event after you've included Simile.Using jQuery:
Using no library (found here):
使用 jQuery 的
$(document).ready
事件,该事件允许您添加任意数量的处理程序(与onload
不同)。Use jQuery's
$(document).ready
event, which lets you add an arbitrary number of handlers (unlikeonload
).添加 jQuery 并使用
在 jQuery 中还有其他几种编写方法,但我发现这是最具解释性的。
如果您正在进行任何类型的 JavaScript 开发,您需要考虑 jQuery。真是太甜了!
Add in jQuery and use
There are a couple of other ways to write this in jQuery, but I find this is the most explanatory.
If you're doing any kind of JavaScript development, you need to be looking at jQuery. It's totally sweet!
除了前面提到的 jQuery 解决方案(但是我强烈建议使用它,它很棒)之外,这里是普通的 JS 解决方案(因为您在问题中没有提到任何有关 jQuery 的内容):
Apart from the aforementioned jQuery solution (I however highly recommend to use it, it's great) here's the plain vanilla JS solution (as you didn't mention anything about jQuery in your question):
所谓劫持,您的意思是 Simile 在您的代码之后加载并覆盖
onload
?在这种情况下,请确保您支持它(如 Justin 说),在你自己覆盖它之前,将onload
的值存储在某个地方,这样它仍然会被调用(从你自己的处理程序)。也就是说,如果您不使用 jQuery。
By hijacked, you mean, Simile loads after your code and overwrites
onload
? In that case, make sure you get behind it (as Justin says), and before you overwrite it yourself, store the value ofonload
somewhere, so it still gets called (from your own handler).That is, if you don't use jQuery.
如果你看这里,你会看到 jQuery 使用的解决方案,我相信,或者它的修改,但它应该可以满足你的需求。
http://dean.edwards.name/weblog/2006/06/again/< /a>
你会看到最后一行被注释掉了。如果事情发展到这一步,要么你会破坏它们(取消注释),要么你的代码将无法运行,但这适用于主要浏览器。
If you look here you will see a solution that is what jQuery uses, I believe, or a modification of it, but it should work for your needs.
http://dean.edwards.name/weblog/2006/06/again/
You will see that the last line is commented out. In the event it gets that far either you will clobber them (uncomment) or your code won't run, but this will work for the major browsers.