Javascript 检查一个窗口是否是弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10240398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether a window is Popup or not?
提问by Jeevan
This question is not the duplicate of if window is popup, But a similar one.
这个问题不是if window is popup的重复,而是类似的问题。
I am developing a extension which injects scripts to all web pages. I need to detect whether the window is popup or not.
我正在开发一个将脚本注入所有网页的扩展程序。我需要检测窗口是否弹出。
Note: I am not the one who is opening the popup window, So the above solution won't work.
注意:我不是打开弹出窗口的人,因此上述解决方案不起作用。
回答by Steve
I've found that some browsers will set window.opener to window in certain cases. This is the most reliable popup check that I am using right now.
我发现某些浏览器在某些情况下会将 window.opener 设置为 window 。这是我现在使用的最可靠的弹出检查。
if (window.opener && window.opener !== window) {
// you are in a popup
}
回答by BarelyFitz
The following worked for me when testing in Chrome, Firefox, Safari, and IE8. It worked for a window created useing window.open() or target=_blank.
在 Chrome、Firefox、Safari 和 IE8 中进行测试时,以下内容对我有用。它适用于使用 window.open() 或 target=_blank 创建的窗口。
if (window.opener) {
alert('inside a pop-up window or target=_blank window');
} else if (window.top !== window.self) {
alert('inside an iframe');
} else {
alert('this is a top level window');
}
回答by Zaucy
window.locationbar.visible
not necessarily only for a popup, but can help detecting if the user can change the location manually via location bar...
window.locationbar.visible
不一定只用于弹出窗口,但可以帮助检测用户是否可以通过位置栏手动更改位置...
回答by BornToCode
Why not just check if the opener is NOT undefined? a popup has an opener while a regular page hasn't.
为什么不检查开瓶器是否未定义?弹出窗口有一个开启器,而普通页面没有。
if (window.opener != null)
回答by Maksym Kozlenko
The following statement will be true if window is a popup window or subframe:
如果 window 是弹出窗口或子框架,则以下语句为真:
window.parent != window
回答by IdontCareAboutReputationPoints
I use this code to determine a popup window
我使用此代码来确定弹出窗口
(window.opener && window.opener !== window && !window.menubar.visible)
回答by Anthony
This is not a sure fire method, but typically a popup using window.open()
does not direct to a new URL, so both windows will have the same href. Therefore, in many cases:
这不是一个确定的方法,但通常使用的弹出窗口window.open()
不会指向新的 URL,因此两个窗口将具有相同的 href。因此,在很多情况下:
window.location.href == window.opener.location.href
will be true for a popup. So you could do:
对于弹出窗口将是 true。所以你可以这样做:
var isPopup = (window.location.href == window.opener.location.href);
Like I said, this works in my limited tests, so there may be some feedback that shows where this won't be reliable.
就像我说的,这在我有限的测试中有效,所以可能会有一些反馈表明这不可靠。
回答by jsbuster
The best way, is simply to check the window width, then try to resize the window width in a pixel or two, and then check if the current width equals or not to the one before the resize
最好的方法是简单地检查窗口宽度,然后尝试以一两个像素为单位调整窗口宽度的大小,然后检查当前宽度是否等于调整大小之前的宽度
in other words, if you succeeded to resize the window, you're probably inside a popup.
换句话说,如果您成功地调整了窗口大小,则您可能在弹出窗口中。
good luck.
祝你好运。
回答by c121hains
For me, i was implementing a Logout screen that could be shown in a window popup via window.open(..) or from a link within a single page.
对我来说,我正在实现一个注销屏幕,它可以通过 window.open(..) 或从单个页面中的链接显示在窗口弹出窗口中。
I needed to determine:
我需要确定:
1) if my Logout screen is within a popup, then:
1) 如果我的注销屏幕在弹出窗口中,则:
event.preventDefault();
window.close();
2) otherwise use the browser back function..
2) 否则使用浏览器返回功能..
window.history.back();
My solution: If the page is within a popup, in my case it has a window page history of 1:
我的解决方案:如果页面在弹出窗口中,就我而言,它的窗口页面历史记录为 1:
event.preventDefault();
if (window.history.length === 1) {
window.close();
} else {
window.history.back();
}
回答by Kamal Singh Kushwaha
I was implementing 'close' button for popup window screen and 'cancel' button for '_self' window in "Typescript/Nodejs" and believe it will work for JavaScript as well. So I needed to determine if screen is opened in a popup
我正在“Typescript/Nodejs”中为弹出窗口屏幕实现“关闭”按钮,为“_self”窗口实现“取消”按钮,并相信它也适用于JavaScript。所以我需要确定屏幕是否在弹出窗口中打开
if(window.opener || window.history.length === 1)
isPopupWindow = true;
else
isPopupWindow = false;
"window.opener" works for almost all the browsers but having issue with IE so to handle IE issue I used "window.history.length".
“window.opener”几乎适用于所有浏览器,但对 IE 有问题,因此为了处理 IE 问题,我使用了“window.history.length”。