JavaScript Pop-under

I recently had to do a pop under with JS and found the below snippet (can’t remember where anymore), I might need it again so I post it here for reference.

var popCookieName = "popunder_24";
var expireHours = "24";
var alreadyExecuted = false;
var browserUserAgent = navigator.userAgent;
var config = "width=1000,toolbar=1,menubar=1,resizable=1,scrollbars=1";

function displayTheWindow() {
	var didPop = false;
	if (alreadyExecuted == false && Get_Cookie(popCookieName) === null) {
		alreadyExecuted = true;
		if (browserUserAgent.search("Firefox/3") > -1) {
			config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
			var w = window.open(urlToShow, 'myWindow', config).blur();
			window.focus();
			if (w) didPop = true
		} else if (browserUserAgent.search("Firefox") > -1) {
			config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
			var w = window.open(urlToShow, 'myWindow', config);
			var temp = w.window.open("about:blank");
			temp.close();
			if (w) didPop = true
		} else if (browserUserAgent.search("Opera") > -1) {
			var w = window.open(urlToShow, 'myWindow', config);
			if (w) didPop = true
		} else if (browserUserAgent.search("Chrome") > -1) {
			config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
			var w = window.open(urlToShow, 'myWindow', config).blur();
			window.focus();
			if (w) didPop = true
		} else if (browserUserAgent.search("MSIE") > -1) {
			config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
			var w = window.open(urlToShow, 'myWindow', config);
			window.setTimeout(window.focus, 750);
			window.setTimeout(window.focus, 850);
			if (w) {
				w.blur();
				didPop = true
			}
		} else if (browserUserAgent.search("Safari") > -1) {
			config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
			var w = window.open(urlToShow, 'myWindow', config).blur();
			if (w) didPop = true
		} else {
			var w = window.open(urlToShow, 'myWindow', config);
			if (w) didPop = true
		}
		Set_Cookie(popCookieName, "1", expireHours)
	}
	alreadyExecuted = true;
}

function Set_Cookie(popCookieName, value, expire_hours) {
	var today = new Date();
	today.setTime(today.getTime());
	var expires_date = new Date(today.getTime() + (1000 * 60 * 60 * expire_hours));
	document.cookie = popCookieName + "=" + escape(value) + ";expires=" + expires_date.toGMTString() + ";path=/"
}
function Get_Cookie(check_name) {
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	for (i = 0; i < a_all_cookies.length; i++) {
		a_temp_cookie = a_all_cookies[i].split('=');
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if (cookie_name == check_name) {
			b_cookie_found = true;
			if (a_temp_cookie.length > 1) {
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''))
			}
			return cookie_value;
			break
		}
		a_temp_cookie = null;
		cookie_name = ''
	}
	if (!b_cookie_found) {
		return null
	}
}

In the php I use the following function to render the initiating code, the target url can be changed by changing the contents of click-url.txt.

function clickPopup(){
	$url = trim(file_get_contents('click-url.txt'));
	if(!empty($url)){ ?>
		<script>
			var urlToShow = <?php echo "'$url'" ?>;
			$(document).ready(function(){
				$("a").unbind('click').bind('click', displayTheWindow);
			});
		</script>	
	<?php }
}
clickPopup();

Related Posts

Tags: ,