Jquery实现广告弹窗(背投),绕过所有浏览器拦截

PopUpAdClass = { };
PopUpAdClass = new function() {
 // Private fields
 var that = this;
 var windowHandles = {};
 var isChrome = /chrome/.test(navigator.userAgent.toLowerCase());

 // Public Members
 this.focus = function(windowHandle) {
 if (!windowHandle) {
 throw new Exception("Window handle can not be null.");
 }

 if (isChrome) {
 windowHandle.blur();
 setTimeout(windowHandle.focus, 0);;
 }
 else {
 windowHandle.focus();
 }
 }

 this.windowExists = function(windowTarget) {
 return windowTarget && windowHandles[windowTarget] && !windowHandles[windowTarget].closed;
 }

 this.open = function(url, windowTarget, windowProperties) {
 // See if we have a window handle and if it's closed or not.
 if (that.windowExists(windowTarget)) {

 // We still have our window object so let's check if the URLs is the same as the one we're trying to load.
 var currentLocation = windowHandles[windowTarget].location;

 if (
 (
 /^http(?:s?):/.test(url) && currentLocation.href !== url
 )
 ||
 (
 // This check is required because the URL might be the same, but absolute,
 // e.g. /Default.aspx ... instead of http://localhost/Default.aspx ...
 !/^http(?:s?):/.test(url) &&
 (currentLocation.pathname + currentLocation.search + currentLocation.hash) !== url
 )
 ) {
 // Not the same URL, so load the new one.
 windowHandles[windowTarget].location = url;
 }

 // Give focus to the window. This works in IE 6/7/8, FireFox, Safari but not Chrome.
 // Well in Chrome it works the first time, but subsequent focus attempts fail,. I believe this is a security feature in Chrome to avoid annoying popups.
 that.focus(windowHandles[windowTarget]);
 }
 else {
 // Need to do this so that tabbed browsers (pretty much all browsers except IE6) actually open a new window
 // as opposed to a tab. By specifying at least one window property, we're guaranteed to have a new window created instead
 // of a tab.
 //windowProperties = windowProperties || 'menubar=yes,location=yes,width=700, height=400, scrollbars=yes, resizable= yes';
 windowProperties = windowProperties || 'menubar=yes,location=yes,width=' + (screen.availWidth - 15) + ', height=' + (screen.availHeight - 140) + ', scrollbars=yes, resizable= yes';
 windowTarget = windowTarget || "_blank";

 // Create a new window.
 var windowHandle = windowProperties ? window.open(url, windowTarget, windowProperties) : window.open(url, windowTarget);

 if (null === windowHandle || !windowHandle) {
 alert("You have a popup blocker enabled. Please allow popups for " + location.protocol + "//" + location.host);
 }
 else {
 if ("_blank" !== windowTarget) {
 // Store the window handle for reuse if a handle was specified.
 windowHandles[windowTarget] = windowHandle;
 windowHandles[windowTarget].focus();
 }
 }
 }
 }
}
调用
$(document).click(function(){
		PopUpAdClass.open("http://www.ghugo.com/popup.html",'_blank');
//这个是背投功能,popup 弹出来来或马上失去焦点,最大限度的兼顾用户体验。
		PopUpAdClass.focus(window);
//popup一次就好啦,整天弹会让人反感滴。
		$(document).unbind("click");
	})

扩展阅读:
http://stackoverflow.com/questions/2758608/window-focus-not-working-in-google-chrome
http://www.planabc.net/2008/03/05/ad_back_pop/
http://www.cnblogs.com/birdshome/archive/2007/12/22/window-open.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注