Opera 中 document.onload 事件中插件的事件侦听器

发布于 2024-08-23 11:08:25 字数 545 浏览 3 评论 0原文

我试图了解一个问题,即插件上的事件侦听器注册在 Opera 中不起作用,除非我延迟它们。

特别是,这不起作用:

document.onload = function() {
    plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
}

虽然通过例如 alert() 稍微延迟了 addEventListener() 调用,但是:

document.onload = function() {
    alert('onload()');
    plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
}

似乎插件仅在 之后加载文档.onload。

作为一个非网络开发人员,我在这里错过了一些简单的东西吗?或者这是一个已知的 Opera 问题并有常见的解决方法?

I am trying to understand an issue where event-listener registration on plugins doesn't work in Opera unless I delay them.

In particular, this doesn't work:

document.onload = function() {
    plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
}

while delaying the addEventListener() call somewhat through e.g. an alert() does:

document.onload = function() {
    alert('onload()');
    plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
}

It seems that plugins are only loaded after document.onload.

As a non-web-developer, am I missing something simple here? Or is this a known Opera problem with a common work-around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟燃烟灭 2024-08-30 11:08:25

一般来说,插件初始化、脚本执行和文档事件处理的时间没有明确指定,这意味着浏览器可能会做不同的事情。

在这种情况下,听起来您需要确保在添加侦听器之前初始化插件。一种方法是检查插件将定义的属性(例如,如果它是 Flash 插件,您可以检查 PercentLoaded 是否已定义,以查看它是否已准备好编写脚本。)如果尚未准备好编写脚本,您可以可以使用超时稍后重试。

在 Opera,我们最近一直在努力与该领域的大多数其他浏览器保持一致,而 Opera 10.50 可能会更适合您。不过,我不确定我们是否已经完全控制了事情 - 听听您的意见在 10.50 中行为是否发生变化将会很有趣。

in general the timing of plugin initialisation, script execution and document event handling isn't well specified, meaning browsers are likely to do different things.

In this case it sounds like you need to make sure the plugin is initialised before you add the listener. One way to do that would be to check for a property the plugin will define (for example, if it was a Flash plugin you could check if PercentLoaded was defined to see if it is ready for scripting.) If not ready for scripting, you could use a timeout to try again a little bit later.

At Opera we've been trying to align with the majority of the other browsers in this area recently, and Opera 10.50 may be working better for you. I'm not sure if we have things fully under control yet though - it would be interesting to hear from you whether behaviour changed in 10.50.

亣腦蒛氧 2024-08-30 11:08:25

我们在 Opera 10.60 中进一步改进了对此的处理,因此该行为更接近其他浏览器。插件初始化和脚本准备就绪。我相信原来的方法现在应该可行了。

We have further improved handling of this in Opera 10.60, so that behavior is much closer to the other browsers wrt. plug-in initialization and script readyness. I believe the original approach should work now.

云雾 2024-08-30 11:08:25

我对 Opera 不太了解,但是你尝试过使用 jquery 的 Ready 函数吗?它的目的是添加一个您想要在 DOM 完全加载后执行的函数,并且它应该可以跨浏览器工作。

$(document).ready(function() {
 plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
});

有关就绪函数的更多信息可以在此处找到

I don't know much about Opera but have you tried using jquery's ready function? It's purpose is to add a function you want executed once the DOM is fully loaded and it should work cross browser.

$(document).ready(function() {
 plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
});

More info about the ready function can be found here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文