跨域访问,跨域调用窗口

目的在与调用不同(子)域名的通用登录窗口,并实现跨域操作
##########主页#######################

var getHost = function () { var host = "null"; var url = window.location.href; var regex = /.*://([^/|:]*).*/; var match = url.match(regex); if (typeof match != "undefined" && null != match) { host = match[1]; } if (typeof host != "undefined" && null != host) { var strAry = host.split("."); if (strAry.length > 1) { host = strAry[strAry.length - 2] + "." + strAry[strAry.length - 1]; } } return host;// host格式 '.xxxx.com' } function IsTestUrl(documentStr) { var v_domain = documentStr; if (/tgxrc|localhost|d:/gi.test(v_domain)) {//是否本地测试域名 return ".txxxx.com"; } else return ".xxxx.com"; } function loginForm() { layer.closeAll(); document.domain = getHost(); layer.open({ type: 2, shade: 0.3, shadeClose: true, fix: false, title: "企业登录", area: ['600px', '380px'], content: "http://pass" + IsTestUrl(document.domain) + "/vip/login", //登录页面地址 调用其他域名的登录地址 close: function (index) { layer.close(index); } }); } function loginLayerOp() { layer.closeAll(); showThisForm(); } var c_cna_Sel_pk = true; function showThisForm() { document.domain = "share" + IsTestUrl(document.domain); layer.open({ type: 2, shade: 0.3, fix: false, title: false, shadeClose: true, area: ['600px', '450px'], content: '/Subject2017/MiddleSenior/PartialShowInfo', close: function (index) { layer.close(index); } }); }

################局部视图页##################

<script type="text/javascript">
var v_domain = document.domain;
if (/txxxx|localhost|d:/gi.test(v_domain)) {//是否本地测试域名
v_domain = ".txxxx.com";
}
else
v_domain = ".xxxx.com";

function loginLayerOp(index) {

layer.closeAll();
}
$("#sureLoginBtn").click(function () {
document.domain = "share" + v_domain;
// layer.closeAll();
parent.loginForm();
});
</script>

 

需要重新 将document.domain 设置回主页所在域名。否则parent将不可用

原文地址:https://www.cnblogs.com/NotEnough/p/7560480.html