PDA

View Full Version : one time popup using cookies



unconventional
May 8th, 2003, 09:54 PM
Hi -

I have used two scripts that I found on the
web - both proport to only load a popup once
per session - one is from pc magazine April
issue, another I will list here - they use
cookies, here is my dir structure on my web
site

root
dir1
dir2
dir3

.js is in root, pages in dir1 and dir2 and
dir3 call it, popup comes up once per
dir hit, not once per site hit!

My site

informationhighwaytohell.com

go to travelogue, then move around, you see
popup is loading more than once - what gives?
The js is in a .js file that gets called
by every page

http://javascriptkit.com/popwin/index.shtml

<script>

//Popup Window Script
//By JavaScript Kit (http://javascriptkit.com)
//JavaScript tutorials and over 400+ free scripts

function openpopup(){
var popurl="http://fullwebaddresshere"
winpops=window.open(popurl,"","width=400,height=338,")
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function loadornot(){
if (get_cookie('poppedup')==''){
openpopup()
document.cookie="poppedup=yes"
}
}

loadornot()
</script>

Mr J
May 17th, 2003, 01:28 PM
Try this one



<SCRIPT language=JavaScript>
<!--
var expDays = 1; // number of days the cookie should last

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,expires) {
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 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('poponce');
if (count == null) {
count++;
SetCookie('poponce', count, exp);

// Action to take
dothis()
}
}

function dothis(){
var windowprops = "left=50,top=150,width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no"; window.open("yourpage.htm", "", windowprops); // Use to load a page
}
// -->
</SCRIPT>

Insert the onLoad event handler into your BODY tag.

<BODY OnLoad="checkCount()">

Rafton
Jul 1st, 2003, 01:36 PM
Do either of these codea allow me to use the information in the popup in the middle of a text? I wish to use a persons name many times on many pages.

eg: " As you know we here at xxxxx.com believe this is the best idea for you. POP UP NAME, if you should agree then please e-mail us. POP UP NAME, please remember to include your reference number.
Regards
Staff"

Do you see what I mean?

Jason Rafton
:rolleyes:

groogruxking40
Aug 13th, 2009, 04:46 PM
Try this one



<SCRIPT language=JavaScript>
<!--
var expDays = 1; // number of days the cookie should last

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,expires) {
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 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('poponce');
if (count == null) {
count++;
SetCookie('poponce', count, exp);

// Action to take
dothis()
}
}

function dothis(){
var windowprops = "left=50,top=150,width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no"; window.open("yourpage.htm", "", windowprops); // Use to load a page
}
// -->
</SCRIPT>

Insert the onLoad event handler into your BODY tag.

<BODY OnLoad="checkCount()">






I tried it.. it works in Firefox but not IE.8.. why is this?

orca8767
Aug 13th, 2009, 05:34 PM
it's because IE is not standards-compliant, and therefore IE fails epically.

groogruxking40
Aug 13th, 2009, 05:38 PM
it's because IE is not standards-compliant, and therefore IE fails epically.

so how do i remedy this?

i just need a simple popup php script or html code so i can get this working for my boss

Philip M
Aug 13th, 2009, 05:45 PM
Most modern browsers block unrequested pop-ups.

You may do better with something like this:-


<div id = "div1">Hi. This will appear if the cookie is not set.</div.>

<script type = "text/javascript">

function getCookie() {
if ( document.cookie.indexOf("mycookie") < 0 ) {
//if not found show the div
document.getElementById("div1").innerHTML = "Hello! This will appear only once per session."
document.cookie = "mycookie = yes"; // set cookie for next time...without expire so session only
}
}

getCookie();

</script>

groogruxking40
Aug 13th, 2009, 06:38 PM
Most modern browsers block unrequested pop-ups.

You may do better with something like this:-


<div id = "div1">Hi. This will appear if the cookie is not set.</div.>

<script type = "text/javascript">

function getCookie() {
if ( document.cookie.indexOf("mycookie") < 0 ) {
//if not found show the div
document.getElementById("div1").innerHTML = "Hello! This will appear only once per session."
document.cookie = "mycookie = yes"; // set cookie for next time...without expire so session only
}
}

getCookie();

</script>


which code do i replace? the whole script? help me please

Philip M
Aug 13th, 2009, 06:58 PM
which code do i replace? the whole script? help me please

Yes, replace the existing script with this one.