首先说一下业务需求:
需要在当前各主流浏览器上从tab标签形式弹出一个页面,尽量不要以单独窗口弹出。
下面是网上搜集并修改、验证后的js代码,以备以后参阅。
function getUnionCookie(B) {
var A = new RegExp(B + "=([^;]+)"), _ = A.exec(document.cookie), $ = "";
if (_ != null) {
try {
$ = decodeURI(_[1]);
}
catch (C) {
}
}
return $;
}
function setUnionCookie(sName, sValue, oExpires, sPath) {
document.cookie = sName + "=" + escape(sValue) + ";expires=" + oExpires.toUTCString() + ";path=" + sPath;
}
//使用cookie控制每隔24小时弹一次,似乎不见效
function updateunionli() {
var exp2 = new Date();
exp2.setTime(exp2.getTime() + 24 * 60 * 60 * 1000);
setUnionCookie("unionli_lc", 4 + 1, exp2, '/');
var poped_statist = new Image();
poped_statist.src = "http://www.anmeiqi.com/logo/anmeiqi.gif"; //广告的统计的url
}
var anmeiqi_url = 'http://www.anmeiqi.com/';//要推广的url
var is_unionli_poped = getUnionCookie('unionlipop');
if (!is_unionli_poped) {
var unionli_pidx = getUnionCookie('unionli_lc');
if (!unionli_pidx)
unionli_pidx = 1;
(function() {
var aa_url = window.anmeiqi_url;
var _has2ap = 0;
var ua = navigator.userAgent;
var browser = {ie: /msie/i.test(ua),ie6: /msie 6/i.test(ua),ie7: /msie 7/i.test(ua),ie8: /msie 8/i.test(ua),ie9: /msie 9/i.test(ua),360: /360se/i.test(ua),sogou: /;?se.+?MetaSr/i.test(ua),maxthon: /Maxthon/i.test(ua),tt: /TencentTraveler/i.test(ua),ff: /firefox/i.test(ua),webkit: /AppleWebKit/i.test(ua),opera: /Opera/i.test(ua),qqbrowser: /QQBrowser/i.test(ua),theworld: /Theworld/i.test(ua)};
function ev_op(e, event, func, act) {
if (browser.ie)
e[act === undefined ? 'attachEvent' : 'detachEvent']('on' + event, func);
else
e[act === undefined ? 'addEventListener' : 'removeEventListener'](event, func, false)
}
//弹窗效果顶层包装函数
function pop(url, param) {
if (!document.body) {
return setTimeout(function() {
pop(url, param)
}, 13)
}
try {
if (browser['webkit'] && browser['maxthon']) {
if (!core_pop(url)) {
a_pop(url)
}
} else if (browser['tt']) {
try {
object_pop(url)
} catch (e) {
a_pop(url)
}
} else if (browser['sogou']) {
if (!core_pop(url)) {
a_pop(url)
}
} else if (browser['webkit'] && browser['qqbrowser']) {
if (!core_pop(url)) {
click_pop(url)
}
} else if (browser['webkit'] || browser['opera']) {
a_pop(url)
} else if (browser['theworld'] && browser.ie6) {
if (!object_pop2(url)) {
a_pop(url)
}
} else if (browser['theworld'] && browser.ie8) {
if (!core_pop(url)) {
try {
object_pop(url)
} catch (e) {
click_pop(url)
}
}
} else if (browser.ie6) {
if (!core_pop(url)) {
object_pop2(url)
}
} else if (browser.ie8) {
if (!core_pop(url)) {
document.onclick = function() {
core_pop(url);
document.onclick = null
}
}
} else if (browser['ie']) {
try {
object_pop(url)
} catch (e) {
click_pop(url)
}
} else if (browser['ff']) {
if (!core_pop(url)) {
click_pop(url)
}
} else {
if (!core_pop(url)) {
click_pop(url)
}
}
_has2ap = 1;
updateunionli()
} catch (e) {
if (browser.ie7 || browser.ie8 || browser.ie9 || browser['qqbrowser']) {
click_pop(url)
} else {
a_pop(url)
}
_has2ap = 1;
updateunionli()
}
}
function append(e) {
for (var t in {body: 1}) {
var ele = document.getElementsByTagName(t);
for (var i = 0; i < ele.length; i++) {
ele[i].insertBefore(e, ele[i].firstChild);
return
}
}
}
function object_pop(url, param) {
var object = document.createElement('object');
object.setAttribute('classid', 'CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6');
object.style.cssText = 'position:absolute;left:1px;top:1px;width:1px;height:1px;';
append(object);
object.launchURL(url)
}
function object_pop2(url, param) {
var object2 = document.createElement('object');
object2.setAttribute('classid', 'clsid:2D360201-FFF5-11d1-8D03-00A0C959BC0A');
object2.style.cssText = 'position:absolute;left:1px;top:1px;width:1px;height:1px;';
append(object2);
for (var i in object2) {
try {
(function(o) {
})(object2[i])
} catch (e) {
}
}
setTimeout(function() {
object2.DOM.Script.open(url, '_blank', '')
}, 500)
}
//监视到mouseup事件后异步取消,然后弹窗
function click_pop(url, param) {
ev_op(document, 'mouseup', function(e) {
e = e || window.event;
e.canceBubble = true;
ev_op(document, 'mouseup', arguments.callee, true);
core_pop(url, param)
})
}
function a_pop(url) {
var a = document.createElement('a');
a.href = url;
a.target = '_blank';
var div = document.createElement('div');
div.style.backgroundColor = '#ffffff';
a.style.filter = 'alpha(opacity=0)';
a.style.opacity = '0';
a.appendChild(div);
append(a);
a.style.display = 'block';
a.style.cursor = 'default';
a.style.position = 'absolute';
a.style.left = '0px';
a.style.top = '0px';
a.style.zIndex = 999999;
var i = setInterval(function() {
var d = document.documentElement || document.body;
a.style.top = Math.max(document.documentElement.scrollTop, document.body.scrollTop) + 'px';
div.style.width = Math.min(d.clientWidth, d.scrollWidth) + 'px';
div.style.height = d.clientHeight + 'px'
}, 200);
a.onclick = function(e) {
setTimeout(function() {
a.parentNode.removeChild(a)
}, 200);
clearInterval(i)
};
a.onmouseup = function(e) {
e = e || window.event;
e.canceBubble = true
}
}
function core_pop(url, param) {
var w = window.open(url, '_blank', 'left=0,top=0,toolbar=yes,location=yes,' + 'status=yes,menubar=yes,scrollbars=yes,' + 'resizable=yes,width=' + screen.width + ',height=' + screen.height);
return w
}
//调用弹窗函数入口
setTimeout(function() {
pop(aa_url, {a: 1,b: 2})
}, 300);
ev_op(window, 'beforeunload', function() {
})
})();
}
本文介绍了一种在不同浏览器中实现兼容性良好的弹窗方法,通过JavaScript检测浏览器类型,并采用不同技术实现在新标签页中打开指定网页的功能。
862

被折叠的 条评论
为什么被折叠?



