Javascript中封装window.open解决不兼容问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Javascript中封装window.open解决不兼容问题

  2020-05-13 我要评论

对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码:

代码如下

var openWindow = function(url, options) {
var str = "";
if (options) {
options.height = options.height || 420;
options.width = options.width || 550;
options.left = options.left || ((screen.width - options.width) / 2); //默认为居中
options.top = options.top || ((screen.height - options.height) / 2); //默认为居中

for (var i in options) {
str += ',' + i + '=' + options[i];
}
str = str.substr(1);
};
window.open(url, 'connect_window_'+ (+new Date), str);//参数1为url,参数2为了能可以重复弹出
str = null;
};


/https://img.qb5200.com/download-x/demo 1:新窗口打开我的led投光灯电源网站
document.body.onclick = function(){
openWindow("http://www.daermay.com/ ?rel=xuexb");
}

/https://img.qb5200.com/download-x/demo 2:固定宽 并居中
document.body.onclick = function(){
openWindow("//www.qb5200.com/ ?rel=xuexb",{
width:888
});
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们