window.opener 不正确 [英] window.opener is not right

查看:30
本文介绍了window.opener 不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开一个弹出窗口,并希望在其中进行单击操作,从而导致在打开它的页面中执行一些 jQuery.我在网上找到的所有内容都表明我应该能够使用 window.opener 做到这一点,(例如 JQuery - Write打开窗口)

I'm opening a popup and want to have a click action in it cause the execution of some jQuery in the page that opened it. Everything I find online says I should be able to do that with window.opener, (e.g. JQuery - Write to opener window)

但是当我 console.log window.opener 时,它只是真实的",而不是真实的对象.window.opener.jQuerywindow.opener.$ 和 'window.opener.document' 一样未定义.

But when I console.log window.opener, it's just 'true', not a real object. window.opener.jQuery is undefined as is window.opener.$ and 'window.opener.document'.

这是打开窗口的代码:

window.open('http://google.com' , "moderatorWindow", 'width=300, height=300');

这是在 Safari 中.其他页面能够启动一个弹出窗口,当我在这些页面上检查 window.opener 时,我得到了一个真实的对象.我做错了什么?

This is in Safari. Other pages are able to launch a popup and when I inspect window.opener on those, I get a real object. What am I doing wrong?

推荐答案

由于同域策略规则,您的变量为 true 而不是对象.就像 iframe 一样,如果您打开的弹出窗口不在同一个域或子域中,那么在您创建它之后它就会丢失.如果我可以说,在我的网站上打开一个(隐藏的)iframe 到 gmail.com 并能够阅读您的电子邮件,那么网络将是一个非常不安全的地方.

Your variable is true and not an object because of same-domain policy rules. Just like an iframe, if the popup you open is not on the same domain or sub-domain then it is lost to you after you create it. The web would be a very unsecure place if I could say, open a (hidden) iframe on my site to gmail.com and was able to read your email.

即使弹出窗口位于子域上,您也必须做额外的工作并将两个窗口的 document.domain 值设置为根域(例如 mydomain.com).这是为了确保弹出的站点希望其父级知道(再次考虑安全性,如果我的 coke.ning.com 社区可以打开一个隐藏的 iframe 到您的 pepsi.ning.com 并蛮力尝试登录等)

Even if the popup is on a sub-domain you have to do extra work and set the document.domain value of both windows to the root domain (eg. mydomain.com). This is to ensure that the popped-up site wants to be known to its parent (again, think security, if my coke.ning.com community could open a hidden iframe to your pepsi.ning.com and do brute force attempts at a login, etc.)

为了证明我的观点,请尝试访问 google.com 并打开 Firebug(如果您使用的是 Safari 或 Chrome,则打开 Inspector)并执行以下操作:

To prove my point try actually going to google.com and opening up Firebug (or Inspector if you're using Safari or Chrome) and doing:

var bob = window.open('http://google.com' , "moderatorWindow", 'width=300, height=300');
bob.window.location.href; // returns "http://www.google.com/"

最后,您可以随意在子页面上调用 jQuery 来修改同一页面上的元素,反之亦然,但您不能在一个页面上使用 jQuery 来修改另一个页面的 dom.几年前我尝试过这个尝试节省一些加载时间,除非有什么改变,否则它不起作用.jQuery 似乎绑定到创建它的窗口对象.奇怪的事情发生了.

Lastly, feel free to call jQuery on the child page to modify elements on the same page and vice-versa but you can't use jQuery from one page to modify the dom of the other page. I tried this a few years ago to try to save on some loading time and, unless something has changed, it doesn't work. jQuery seems to be bound to the window object of where it was created. Weird things happen.

这篇关于window.opener 不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆