如何识别我的域打开了弹窗window?
How to identify that my domain has opened the popup window?
所以有一个 url 像:
https://subdomain.domain.com/popuppage
我在弹出 window 中打开它使用:
window.open("https://subdomain.domain.com/popuppage", null, "height=600,width=800")
现在点击第三方网站的 link 后,同一页面会在新标签页中打开。问题是我想确定它是由我或第三方网站打开的。有什么办法吗?
我尝试检查 window.opener
属性 但在这两种情况下我都得到 window.opener
并且浏览器不允许我访问任何 属性 of window.opener
因为 CORS 政策。
您可以尝试访问window.opener.location.href
。如果它有效,它是同一个域(因此没有第三方),如果它失败,它很可能不是(因此第三方)。
window.onload = function(){
var _CORS = null;
try{
window.opener && window.opener.location.href;
_CORS = false
//REM: Edit according to comment
/*
_CORS = !(
!window.opener ||
(window.opener.location.href && window.location.host === window.opener.location.host)
)
*/
}
catch(err){
_CORS = true
};
alert('Third party: ' + _CORS)
};
将这个放在 https://subdomain.domain.com/popuppage
中 <head>
或者你想进一步评估它。
所以有一个 url 像:
https://subdomain.domain.com/popuppage
我在弹出 window 中打开它使用:
window.open("https://subdomain.domain.com/popuppage", null, "height=600,width=800")
现在点击第三方网站的 link 后,同一页面会在新标签页中打开。问题是我想确定它是由我或第三方网站打开的。有什么办法吗?
我尝试检查 window.opener
属性 但在这两种情况下我都得到 window.opener
并且浏览器不允许我访问任何 属性 of window.opener
因为 CORS 政策。
您可以尝试访问window.opener.location.href
。如果它有效,它是同一个域(因此没有第三方),如果它失败,它很可能不是(因此第三方)。
window.onload = function(){
var _CORS = null;
try{
window.opener && window.opener.location.href;
_CORS = false
//REM: Edit according to comment
/*
_CORS = !(
!window.opener ||
(window.opener.location.href && window.location.host === window.opener.location.host)
)
*/
}
catch(err){
_CORS = true
};
alert('Third party: ' + _CORS)
};
将这个放在 https://subdomain.domain.com/popuppage
中 <head>
或者你想进一步评估它。