Home >Web Front-end >JS Tutorial >Encapsulating window.open in Javascript solves incompatibility issues_jquery
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 }); }