PDA

View Full Version : Pop Up on Exiit


dedwards62
08-16-2002, 06:46 PM
I am new to Java script and I am looking to create a pop up window when someone leaves the web page I have the following code set up so it pops up when the page loads:
<script>
function openpopup(){
var popurl="thankyou.htm"
winpops=window.open(popurl,"","width=400,height=338,")
}

openpopup()

</script>

how can I change it so it only pop's up when they leave......I hope this makes sence....

beetle
08-16-2002, 06:56 PM
There have been several posts about this topic here. You need to utilize cookies to get it done right.

http://www.grunionweb.com/halloween/javascript.html

3rd one down....

x_goose_x
08-16-2002, 09:16 PM
try:

<body onunload="myfunction();">

beetle
08-16-2002, 09:21 PM
onUnLoad() won't work, because it fires when[list=1] Browser exits
Page is refreshed/reloaded
Page hop via HTML or scripting
[/list=1] Sooo, basically your "exit" popup would show up pretty much all the time if you just used onUnLoad()

x_goose_x
08-16-2002, 09:46 PM
Isn't that what he wants?

beetle
08-16-2002, 09:56 PM
Originally posted by dedwards62
how can I change it so it only pop's up when they leave...I don't think so. We better wait to hear from him...:D

dedwards62
08-16-2002, 10:21 PM
please help me with the placement of Unload I am very new at this

beetle
08-16-2002, 10:28 PM
<script>
function openpopup(){
var popurl="thankyou.htm"
winpops=window.open(popurl,"","width=400,height=338,")
}
</script>
</head>
<body onUnload="openpopup();">

dedwards62
08-16-2002, 10:36 PM
Bettle is correct that thing pops up all the time now....:)

I can work but I think people will get pissed off if it kept poping up like that......

But the script using cookies didn't seem to work.......I just cut and pasted it into my html is there more I need to do?

beetle
08-16-2002, 10:42 PM
Of course! It's never simple enough to just cut and paste. I colored the modifications in red <script LANGUAGE="JavaScript"><!-- Begin
var expDays = 1; // number of days the cookie should last

var page = "thankyou.htm";
var windowprops = "width=400,height=338";

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 openpopup() {
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>Note: This will replace the existing script I posted earlier. Just keep the onUnload event on the BODY tag, and you're set.

redhead
08-16-2002, 10:44 PM
you can generate pop up windows here (http://javascriptkit.com/popwin/index.shtml)... you can choose whether you want it popping up once (using cookies) or all the time...

hope that helps:thumbsup:

dedwards62
08-16-2002, 11:05 PM
Thanks for all your help!!!!:cool: