View Full Version : Resolved How to make a window pop under the current window onload
raideris21
04-24-2009, 11:16 PM
Hello all someone can remake this script that opened new windows other site. When a visitor open my website +automatically opens another website in new window one time per IP address? Sorry for the English spelling.
I have script
Insert the following code into the head section of your html document:
<SCRIPT LANGUAGE='JAVASCRIPT' TYPE='TEXT/JAVASCRIPT'>
<!--
var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus){
if(GetCookie("sid") == "999"){return;}
document.cookie="sid=999; Path=/; Expires= " + getFuture(999);
if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";win=window.open(mypage,myname,settings);
win.blur();}
// Cookie functions borrowed from:
// Ronnie Moore: Visit him at http://www.ronniemoore.com
function getFuture(f){
var d = new Date();
d.setTime(d.getTime() + (86400000 * f));
return d;
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
// -->
</script>
code to the <BODY> tag of your document.
<body onLoad="NewWindow('http://www.google.com','acepopup','640','480','fullscreen','back');"return false " onFocus= "this.blur();">
P.S. This blocks the browser and can not open a new window. Help me please thank you.
raideris21
04-28-2009, 09:52 AM
This is no one knows?
jscheuer1
04-28-2009, 04:22 PM
Somebody must know, because mlb.com does a pop under onload (or at least during page load) that the browser doesn't block. However, with the exception of at least IE 7, I cannot figure out how to do this. From what I can tell from looking at the MLB site's javascript code, this is done by a company called Casale Media. A script on mlb.com writes an external script tag to a script on the Casale Media server. This script, if examined, has no code, and in fact the pop under launched has this script's src attribute as its URL, so they must be using a server side language to convert the script to nothing while using its src attribute to launch the pop under.
For IE 7 (probably other IE versions but perhaps not all) one may use jQuery 1.3's document ready function to simulate a click on a link that pops up or under:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus){
if(readCookie("sid")){return;}
createCookie("sid", "sid");
if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";win=window.open(mypage,myname,settings);
win.blur();}
// Cookie functions borrowed from:
// http://www.quirksmode.org/js/cookies.html#script
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
$(document).ready(function () {
$("#cookwin").click();
});
// -->
</script>
<style type="text/css">
#cookwin {
display: none;
}
</style>
</head>
<body>
<a id="cookwin" href="http://www.google.com" onclick="NewWindow(this.href,'acepopup','640','480','fullscreen','back');return false;">Test</a>
</body>
</html>
In the above pop under code, change win.blur() to win.focus() for a pop up.
raideris21
04-29-2009, 09:08 AM
Thank you
jscheuer1
04-29-2009, 09:26 AM
Something I forgot to mention is that you can always force a pop up or pop under in any browser with default configuration as long as the user clicks on something on the page. Once a user clicks, default browser configs will allow a new window.
raideris21
05-02-2009, 10:17 AM
Something I forgot to mention is that you can always force a pop up or pop under in any browser with default configuration as long as the user clicks on something on the page. Once a user clicks, default browser configs will allow a new window.
Maybe you can show the script?
jscheuer1
05-03-2009, 07:21 AM
It's not all that different. This one is once again a pop under, but may be modified to be a pop up in the same way as mentioned previously. The link on the page is to Yahoo, but if you click on it, or anywhere on the page, there will be a pop under to Google, any link that is clicked will load as normal, unless it is a dummy or disabled, but the pop up/under will also fire, as long as the cookie hasn't been set:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus){
if(readCookie("sid")){return;}
createCookie("sid", "sid");
if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";win=window.open(mypage,myname,settings);
win.blur();}
// Cookie functions borrowed from:
// http://www.quirksmode.org/js/cookies.html#script
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
$(document).ready(function () {
$(document).click(function (){$("#cookwin").click();});
});
// -->
</script>
<style type="text/css">
#cookwin {
display: none;
}
</style>
</head>
<body>
<a id="cookwin" href="http://www.google.com" onclick="NewWindow(this.href,'acepopup','640','480','fullscreen','back');return false;">Test</a>
<a href="http://www.yahoo.com/">test</a>
</body>
</html>
raideris21
05-04-2009, 08:34 AM
Thank you
redhatlab
01-14-2010, 04:41 AM
Hi,
The code:
$(document).click(function (){$("#cookwin").click();});
Shows an error in IE 7 and 8.
Thank you
jscheuer1
01-14-2010, 09:38 AM
I'm not sure why that didn't come up before. Perhaps because in 'normal' use the page navigates away from the error. Anyways though, having a click on the document trigger a click on an element will then trigger the click on the document again, etc. It's an endless loop. Most browsers will barf at that in one way or another, add the highlighted:
$(document).ready(function () {
$('#cookwin').click(function(e){e.stopPropagation();});
$(document).click(function (){$("#cookwin").click();});
});
After that, I'm getting no error here in IE 7.
redhatlab
03-01-2010, 03:55 PM
Hi,
Thank you for the quick reply and I am sorry it took me so long to post back.
Your fix took care of the memory issues on IE 6, 7 and 8.
I just noticed that on IE6 the pop under is being block as a regular pop up, is that ok?
Should I disable the script on IE6?
Once again thank you.
jscheuer1
03-01-2010, 06:28 PM
That would depend upon the pop up blocking settings in IE 6. If they are the same as those in the other browsers where this works, I'd say yes, disable it in IE 6. If they are more strict, just live with it. Most users will probably still see it in IE 6. This entire concept relies upon the browser having the default pop up blocking settings. The strictest settings will still block it in any browser.
In any case, as I believe I said somewhere else in this thread, if this is a mission critical pop up or pop under, a commercial service is available for it. I also believe, and I did not mention this in this thread, that if one has a valid up to date certificate for one's code, it will also be able to override the user's settings. I'm not sure about this, but I've looked into it a bit. It looks promising, but complicated. And one needs to obtain the certificate from a third party. Said certificate must be kept current as well. I'm pretty sure there is a fee involved. Just having the cert is not enough though, one must also know how to use it, the code for it is a bit different than regular script code.
redhatlab
03-01-2010, 09:36 PM
Hi,
Thank you for the inside. I was checking the server logs looking for the browser usage and I notice that IE6 usage has decrease tremendously lately so it might just disable it for IE6.
A question regarding jQuery. Is it there any good tutorial I can use to crack it? I have been using PHP/MySQL for a while and I migrating most of my sites to Drupal and they use jQuery, but I just don't get it.
Thank you.
jscheuer1
03-02-2010, 01:54 AM
Though not required, a thorough understanding of basic javascript is helpful in understanding jQuery.
My web resource for jQuery are the official docs, specifically the API:
http://api.jquery.com/
Things are not spelled out in detail, but there are many examples from which one can glean an understanding, as well as specific usage instructions for the various methods.
One thing about jQuery that may not be readily apparent is the fact that each method usually returns an object that may be chained to another method, in turn returning another or the same object that may also then be chained, and on and on if you like. For example:
$('img').hide().first().fadeIn();
This will take all images and make them display: none, then find the first one and fade it in.
The other basic concept that often eludes folks is the fact that when using:
$('whatever').something();
whatever can be any valid selector as used in ordinary css, as well as numerous additional jQuery specific selectors that are css-like. It will return a collection of elements exactly as a css selector would. Only instead of only styling them (though that is also possible), whatever follows the selector will be performed upon each element that qualifies.
redhatlab
03-17-2010, 08:07 PM
Hi,
I was doing my own modification to the script to have the pop under show when the user leave the page using the back button, but it is not working. What did I miss?
$(document).unload(function () {
$('#cookwin').click(function(e){e.stopPropagation();});
$(document).click(function (){$("#cookwin").click();});
});
Thank you and thank you for the jQuery tips.
jscheuer1
03-18-2010, 07:07 AM
The onload and onunload work a little differently. You have to setup the:
$(document).click(function (){$("#cookwin").click();});
before the unload event fires, otherwise it will be too late. But you might not even really want that.
I haven't tested this. But I'm pretty sure that if this can work at all, you would have to set up the 'proxy' click like so:
$(function(){
$('#cookwin').click(function(e){e.stopPropagation();});
$(document).unload(function (){$("#cookwin").click();});
});
or:
$(function(){
$('#cookwin').click(function(e){e.stopPropagation();});
$(window).unload(function (){$("#cookwin").click();});
});
One of the two should get it.
These both assume a unique element with the unique id specified, that also already has a desirable, pre-existing click event.
redhatlab
03-18-2010, 04:12 PM
Hi John,
I try them both and none of them work IE8, FF and Chrome.
I was trying to send you a PM, but I couldn't.
jscheuer1
03-18-2010, 06:13 PM
Won't work, I just tested it. You cannot have unload trigger a pop up or under. It must be a click. The original method in this thread works because, even though the user isn't clicking on our cookwin link, they are clicking on something.
redhatlab
03-19-2010, 01:47 PM
Hi John,
I even try adding the function to the body like this:
<body onUnload="NewWindow('http://url.com','acepopup','800','600','fullscreen','back');return false; " >
and it is not doing it either.
BTW did you got my email?
Thank you
jscheuer1
03-19-2010, 03:53 PM
Yes. You cannot initiate a new window via javascript unless the browser's security settings are at or below the default settings for pop ups (which includes pop unders) and the browser perceives the the pop up as being initiated by a user click.
Some commercial sites do get around this, but not by javascript alone. It requires a code certificate. And I just checked the one site that I know of (mlb.com) that was doing this, and it is now apparently blocked in Firefox 3.6 and IE 8. So perhaps even that technology has limitations. Either that, or they've stopped doing it that way because it became too expensive (the certificates are not unlike ssl certificates, and must be renewed each year, perhaps they are waiting for the season to begin), or they lost the account or cert. It was a pop under for NetFlix.
If you find one of these sites that does this, you may be able to view their source code to determine what script is doing this, and by tracing it back a bit determine which company is responsible for both the pop under and, more importantly, which company is providing the unblocked pop under services. It would be the latter you would want to find if you are interested in purchasing such a service.
The ones who were/are (though if they still are, it's now ineffective) doing this for MLB are (I think) casalemedia.com. You could check out their site to see if they have such a service. But before you put your money on the line, make sure what they do still works in your target browsers.
redhatlab
03-19-2010, 10:40 PM
Hi John,
Once again thank you for your response.
When you mention the certificate do you mean the SSL certificates like CA or VeriSign?
I am right now scanning the internet looking for a site that does the pop under when back clicking. I went to mlb and IE block the pop up, I guess they are having some issues there.
I found a code some how similar to yours because it use a cookie to fire up the popup and I wonder if that will help me. Here it is:
<script LANGUAGE="JavaScript">
<!-- Begin
var expDays = 90; // number of days the cookie should last
var page = "some_popup.aspx";
var windowprops = "width=359,height=450,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no";
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
} else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function checkCount() {
var count = GetCookie('count');
if (count == null) {
count=1;
SetCookie('count', count, exp);
window.open(page, "", windowprops);
} else {
count++;
SetCookie('count', count, exp);
}
}
// End -->
</script>
And then
<body OnUnLoad="checkCount()">
Thank you
jscheuer1
03-20-2010, 08:40 PM
The cert I'm talking about is like an ssl cert, but it's different. It covers your code. It tells the browser in effect that your code can be trusted, not to block it. An ssl cert only tells the browser that information passed via GET, POST and perhaps other server side variables is secure.
I ran across this Googling one day:
http://msdn.microsoft.com/en-us/library/ms970704.aspx
Just having the cert (actually known as a sign) isn't enough, it must be integrated into the coding and looks quite complex to me.
But I suspect that for things like pop ups, even a signed script may not get past some browsers, at least not anymore. Pop ups are really annoying.
As for the bit about a cookie. Having a cookie doesn't change anything. Unless the user activates the pop up by clicking on a link, or at least the browser thinks the user activates it by clicking on a link, it will usually be blocked. You can set a cookie onunload to set a value that will tell the browser how to process a click when the user returns. But when the user returns to the page, no pop up can happen unless the user clicks on something.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.