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

查看:127
本文介绍了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时,它只是"true",而不是真实的对象. 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社区可以向您的pepsi.ning.com打开一个隐藏的iframe并尝试暴力破解该网站,登录等)

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,则打开Inbugtor)并执行以下操作:

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天全站免登陆