Home > Web Front-end > JS Tutorial > Encapsulating window.open in Javascript solves incompatibility issues_jquery

Encapsulating window.open in Javascript solves incompatibility issues_jquery

WBOY
Release: 2016-05-16 16:35:10
Original
1333 people have browsed it

Encapsulate window.open to make it easier to use and more compatible. Many people say that window.open is incompatible, but it is not, because it cannot be executed directly and must be manually triggered by the user; look at the code:

The code is as follows

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;
};


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

//demo 2:固定宽 并居中
document.body.onclick = function(){
openWindow("http://www.jb51.net/ ?rel=xuexb",{
width:888
});
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template