如何从 Firefox 侧边栏调用 javascript 链接?
主文档中的网站包含一个使用以下语句的 javascript 文件:
<script src="/somescript.js" type="text/javascript"></script>
网站上还有一个链接,用于执行包含文件中定义的 javascript 函数。它看起来像:
<a href="javascript:functionname('parm1', 'parm2');" <img src="http://.../button.png" id="pushme" alt="" height="80" width="200"> </a>
如何在 Firefox 侧边栏的主文档上下文中执行 functionname('parm1', 'parm2');
?
到目前为止我尝试过的是: mainWindow[functionname]
获取指向js函数的指针,但它总是返回null,即使页面已完全加载。 (发生这种情况是因为 javascript 源代码包含在单独的文件中吗?) contentDocument.getElementById("parentdiv").getElementsByTagName('a')[0].click();
调用该方法,但 click 似乎不可用。 (有没有办法让链接可以点击()?)
或者还有其他方法来执行js函数吗?
感谢您的支持!
the website in the main document includes a javascript file using the following statement:
<script src="/somescript.js" type="text/javascript"></script>
There's also a link on the website that executes a javascript function defined in the incuded file. It looks like:
<a href="javascript:functionname('parm1', 'parm2');" <img src="http://.../button.png" id="pushme" alt="" height="80" width="200"> </a>
How can i execute functionname('parm1', 'parm2');
in the context of the main document from a firefox sidebar?
What i've tried so far is:mainWindow[functionname]
to obtain a pointer to the js function, but it always returns null, even if the page was loaded completely. (Does this happen because the javascript source is included from a seperate file?)contentDocument.getElementById("parentdiv").getElementsByTagName('a')[0].click();
to invoke the method, but click does not seem available. (Is there a way to make the link click()-able?)
Or is there an other way to get the js function executed?
Thanks for your support!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法获取窗口的句柄,因为它不是当前窗口的实例变量。您可以使用 window.open() 并将结果句柄存储在变量中来获取对窗口的引用。
无论文件从哪里包含,函数在执行其定义时就会在浏览器的脑海中形成。
要使链接“可点击”,您只需使用窗口句柄调用它即可:
当然,我们不应该使用内联 javascript,但作为一个简单的示例是可以的。 :)
you can't get a handle to the window because it isn't an instance variable of the current window. You can get a reference to the window by using
window.open()
and storing the resulting handle in variable.It doesn't matter where the file is included from, the function comes into being in the browser's mind when its definition is executed.
To make the link 'clickable' you can just call it with the window handle:
Of course, we shouldn't be using inline javascript, but its ok for a quick example. :)