如果打开弹出窗口的网站和在弹出窗口中打开的网站之间的域、协议和端口不匹配,那么您的代码将违反同源策略,要么被忽略,要么抛出异常。
这是一个示例来演示:
<!doctype html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript">
openPopUp = function(href) {
var props = "width=500,height=500,menubar=no,toolbar=no,scrollbars=yes";
var win = window.open(href, href, props);
if (win == null) alert("Your popup blocker is ruining my demo.");
return win;
};
openMe = function(url){
href = (url=="")?document.location:url;
pu = openPopUp(href);
//This will be ignored silently if Same Origin Policy is violated
pu.onload = function() {
p = pu.document.createElement("p");
p.appendChild(pu.document.createTextNode("onload was here."));
pu.document.body.appendChild(p);
};
//This will throw an exception if Same Origin Policy is violated
setTimeout(function() {
p = pu.document.createElement("p");
p.appendChild(pu.document.createTextNode("setTimeout was here."));
pu.document.body.appendChild(p);
},3000);
return false;
}
</script>
<body>
<a href="" onclick="return openMe(this.href);">Self</a>
<a href="https://www.google.com/" onclick="return openMe(this.href);">Google</a>