Javascript encapsulates window.open to solve incompatibilities

  • 2020-03-30 04:04:39
  • OfStack

Window.open is encapsulated to make it more usable and compatible. Many people say that window.open is not compatible. Look at the code:

The following code


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); //The default is center
options.top = options.top || ((screen.height - options.height) / 2); //The default is center

for (var i in options) {
str += ',' + i + '=' + options[i];
}
str = str.substr(1);
};
window.open(url, 'connect_window_'+ (+new Date), str);//Parameter 1 is the url, and parameter 2 can be repeated in order to pop up
str = null;
};


//Demo 1: new window opens my led projector power website
document.body.onclick = function(){
openWindow("http://www.daermay.com/ ?rel=xuexb");
}

//Demo 2: fixed width and center
document.body.onclick = function(){
openWindow("//www.jb51.net/ ?rel=xuexb",{
width:888
});
}

Related articles: