Firefox 扩展中的对象字面量

发布于 2024-10-04 02:56:47 字数 339 浏览 3 评论 0原文

我有一个扩展需要弹出一个窗口然后关闭它。

 var my_extension = {
    window: null,
    popup: function(){
       my_extension.window = window.open(...)
    },
    close: function(){
       my_extension.window.close()
    }
 }

弹出窗口调用关闭函数。但是,从打开返回后,my_extension.window 为空。我检查以确保它已在弹出窗口中设置。弹出窗口返回时是否创建了 my_extension 的另一个实例?

I have an extension that needs to pop up a window and then close it.

 var my_extension = {
    window: null,
    popup: function(){
       my_extension.window = window.open(...)
    },
    close: function(){
       my_extension.window.close()
    }
 }

The popup calls the close function. However, after returning from the open, the my_extension.window is null. I check to make sure it is set in popup. Is another instance of my_extension created when the popup returns?

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

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

发布评论

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

评论(1

浅浅 2024-10-11 02:56:47

my_extension 是在主浏览器窗口中定义的,而不是在弹出窗口中定义的。要从弹出窗口本身关闭弹出窗口,只需使用 window.close

编辑:好的,所以我猜 my_extension.close 实际上看起来像:

function() {
  // check input from popup window
  if (everythingIsGood) {
    my_extension.window.close()
  }
}

在这种情况下,我建议您在弹出窗口中进行验证本身。我知道,您不想在弹出窗口中放置大量代码。我同意。但是,当您打开弹出窗口时,您可以传入进行验证所需的任何信息(包括传递验证函数 - 请记住函数也是对象,因为 JavaScript 就是这么酷!)。在此页面上查找 window.arguments 的讨论:https: //developer.mozilla.org/en/DOM/window.openDialog

my_extension is defined in the main browser window, not in the popup. To close the popup from the popup itself, just use window.close

edit: ok, so I guess my_extension.close actually looks something like:

function() {
  // check input from popup window
  if (everythingIsGood) {
    my_extension.window.close()
  }
}

In that case, I would recommend you do that validation in the popup itself. I know, you don't want to put a lot of code in the popup window. And I agree. But you can pass in whatever information is necessary to do the validation (including passing a validation function -- remember that functions are objects too, because JavaScript is cool like that!) when you open the popup. Look for the discussion of window.arguments on this page: https://developer.mozilla.org/en/DOM/window.openDialog

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