如何让侧边栏执行主文档中的 javascript 代码?
我正在开发一个在主文档中加载网页的 Firefox 侧边栏。 现在我想从主文档调用一个 javascript 函数来响应侧边栏上的按钮。 侧边栏如何使主文档执行其 JavaScript 函数之一? 到目前为止,我有一个包含 javascript 代码的变量,但是如何执行它呢? (在加载的网站的上下文中?)
I'm working on a firefox sidebar that loads a webpage in the main document.
Now i'd like to call a javascript function from the main document in response of a pushed button on the sidebar.
How can the sidebar cause the main document to execute one of its javascript functions?
So far, I'm having a variable containing the javascript code, but how can it be executed? (in the context of the loaded website?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Molle 博士的回答是正确的,但我总是喜欢工作示例。
示例页面 => [已删除。请参阅下面的更新]
结果:
注意:
我必须允许弹出窗口才能使演示代码正常工作,但这是另一个问题的另一个问题。 ; )
更新:
所以这样做
有效,因为它不必等待页面完成加载以使其正常工作。为了能够调用已加载页面上的脚本函数,您必须确保脚本已完成加载。为了说明这一点,我改变了我的例子。
示例页面 => http://mikegrace. s3.amazonaws.com/forums/stack-overflow/example-call-function-in-opened-page.html
JS 看起来像这样
控制台输出
打开的页面
Dr.Molle is correct with his answer but I always love working examples.
Example page => [Removed. See update below]
Results:
Note:
I had to allow popups for the demo code to work but that is another issue for another SO question. ; )
UPDATE:
So doing
worked because it didn't have to wait for the page to finish loading for it to work. In order to be able to call a function is a script on the loaded page you have to ensure that the script has finished loading. To do illustrate this I changed my example.
Example page => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-call-function-in-opened-page.html
The JS looks like this
Console output
Opened page
使用 window.open() 加载页面并将其保存到变量中:
然后您可以使用该变量访问窗口并调用其中的函数:
Load the page using window.open() and save it into a variable:
then you can access the window using this variable and call functions inside: