(function (win) {
var isSupportPlaceholder = function () {
var input = document.createElement('input');
return 'placeholder' in input;
};
//判断是否是IE浏览器,包括Edge浏览器
var IEVersion = function() {
//取得浏览器的userAgent字符串
var userAgent = navigator.userAgent;
//判断是否IE浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion < 10 || !isSupportPlaceholder()) {
return true;
}
}
return false;
};
var Ajax = {
get: function(url, fn) {
// XMLHttpRequest对象用于在后台与服务器交换数据
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
// readyState == 4说明请求已完成
if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
// 从服务器获得数据
fn.call(this, xhr.responseText);
}
};
xhr.send();
}
};
var Brand = {
init: function (config , url , html) {
try {
var iframe = document.createElement('');
} catch (e) {
var iframe = document.createElement('iframe');
iframe.name = 'brandFrame';
iframe.id = 'brandFrame';
}
iframe.frameborder = "0";
iframe.scrolling = "no";
if(url != '') {
iframe.src = url;
}
var style = {
position: "fixed", "border": "0px",
"z-index": "2147483647", "height": ( config.style === 2 ? "250px" : "420px"), "width": "250px"
};
var dotop = config.num;
style["top"] = this.getTop(config.style);
switch (config.pos) {
case 1 :
style["left"] = "0px";
break;
case 2 :
style["right"] = "0px";
break;
}
style["marginTop"] = dotop + "px";
this.style(iframe, style);
document.body.appendChild(iframe);
if(url == '') {
var iframedocument = document.all ? iframe.contentWindow.document : iframe.contentDocument;
iframedocument.open();
iframedocument.write(html);
iframedocument.close();
}
},
style: function (obj, style) {
for (var i in style) {
obj.style[i] = style[i];
}
},
getTop : function (styleType) {
var myHeight = 0;
if (typeof(window.innerHeight) == 'number') {
//Non-IE
myHeight = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myHeight = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientHeight)) {
//IE 4 compatible
myHeight = document.body.clientHeight;
}
var height = styleType === 2 ? 250 : 420;
return parseFloat((myHeight - height) / myHeight / 2).toFixed(2) * 100 + "%";
}
};
//执行创建
var runBrand = function () {
if(IEVersion()) {
Brand.init({"style":1, "pos":1, "num":0} , 'https://www.yxbrand.com/popshow.html?sn=YXPP0010108&style=1&pos=1' , '');
} else {
Ajax.get('https://www.yxbrand.com/popshow.html?sn=YXPP0010108&style=1&pos=1', function (html) {
Brand.init({"style":1, "pos":1, "num":0} , '' , html);
});
}
};
runBrand();
//安全性检测,假如发现对象不见了或者body为空了,则重新画
var checkBrandSafe = function () {
var needRun = false;
var hasIframe = false;
var iframmeCheck = document.getElementById("brandFrame");
if(!iframmeCheck || iframmeCheck === undefined || iframmeCheck === null) {
needRun = true;
} else {
hasIframe = true;
var iframeBody = window.frames["brandFrame"].document.getElementsByTagName("body");
if(!iframeBody || iframeBody === undefined || iframeBody === null || iframeBody.length < 1) {
needRun = true;
} else {
var html = iframeBody[0].innerHTML;
if(!html || html.indexOf("品牌") === -1) {
needRun = true;
}
}
}
if(needRun) {
if(hasIframe) {
iframmeCheck.parentNode.removeChild(iframmeCheck);
}
runBrand();
}
setTimeout(checkBrandSafe , 3000);
};
setTimeout(checkBrandSafe , 1000);
})(window);