如何从 Firefox 侧边栏调用 javascript 链接?

发布于 2024-10-05 16:25:02 字数 748 浏览 1 评论 0原文

主文档中的网站包含一个使用以下语句的 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 技术交流群。

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

发布评论

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

评论(1

烟酉 2024-10-12 16:25:02

您无法获取窗口的句柄,因为它不是当前窗口的实例变量。您可以使用 window.open() 并将结果句柄存储在变量中来获取对窗口的引用。

到目前为止我尝试过的是:
mainWindow[函数名] 获取
指向js函数的指针,但是它
始终返回 null,即使页面
已完全加载。 (这是否
发生的原因是 javascript 源
包含在单独的文件中吗?

无论文件从哪里包含,函数在执行其定义时就会在浏览器的脑海中形成。

要使链接“可点击”,您只需使用窗口句柄调用它即可:

var myWindow = window.open("foo.html");

<a href="javascript:myWindow.functionname('parm1', 'parm2');"> 

当然,我们不应该使用内联 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.

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?

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:

var myWindow = window.open("foo.html");

<a href="javascript:myWindow.functionname('parm1', 'parm2');"> 

Of course, we shouldn't be using inline javascript, but its ok for a quick example. :)

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