Firefox 扩展中的对象字面量
我有一个扩展需要弹出一个窗口然后关闭它。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
my_extension 是在主浏览器窗口中定义的,而不是在弹出窗口中定义的。要从弹出窗口本身关闭弹出窗口,只需使用 window.close
编辑:好的,所以我猜
my_extension.close
实际上看起来像:在这种情况下,我建议您在弹出窗口中进行验证本身。我知道,您不想在弹出窗口中放置大量代码。我同意。但是,当您打开弹出窗口时,您可以传入进行验证所需的任何信息(包括传递验证函数 - 请记住函数也是对象,因为 JavaScript 就是这么酷!)。在此页面上查找
window.arguments
的讨论:https: //developer.mozilla.org/en/DOM/window.openDialogmy_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: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