Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I stop a popup from reloading itself? 1

Status
Not open for further replies.

robert89

Technical User
Nov 19, 2003
125
CA
I have a popup which loads when the page loads. The onload command is used.

Once a visitor has acknowledged the popup i.e. closed it, or followed the link in it, I would like the popup not to reappear should the visitor return to the page where the popup firsts loads.

Is there a way to do this?

Any help would be appreciated.
Bob
 
Bob,

You could set a cookie once the popup has shown, and test for the presence of that cookie before showing it again.

Dan
 
To elaborate on what BillyRayPreachersSon had mentioned, there are different ways to set a cookie. If you are using a scripting language (ASP, PHP, etc), than you can test for the ccokie and, if it exists, dont, display the onload, otherwise, set the cookie and display the onload function.

If you want to use javascript, here is a page that explains pretty well how to accomplish this. All you really need to to write:

document.cookie = "popup=true";

in order to set the cookie. The functions in the page above allow you to retrieve, set the expiration date and so on. So take a look at it.
 
I don't understand the conntection between the link to the script/example that twenerz has provided and the previous reply advising of

document.cookie = "popup=true"

Do I replace

document.cookie = curCookie;

in the sample script with
document.cookie = "popup=true"

and if so, is the value for popup my popup window name or the url

I am new at this game and don't have an intuitive understanding.

Further help would be appreciated.

Bob
 
I apologize for not getting back to you sooner. I was away for a while. I hope you figured it out but just incase you didn't I'll try to explain a little.

When setting cookies, the bar minimum you need is the key and value (i.e. popup=true) 'popup' is the user-defined key and 'true' is the user-defined value. The functions in the link make you life easier by doing it for you. You will use the setCookie and getCookie functions to set the cookie.

In your onload function, first check for your 'popup' cookie to see if it was set. If not, then pop up your window and set the cookie. If it is set, do nothing.


<script>
function popup(){
if (!getCookie('popup')) {
setCookie('popup','true');
window.open('pathToYourWindow','newWindow','arguments');
}
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + &quot;=&quot; + escape(value) +
((expires) ? &quot;; expires=&quot; + expires.toGMTString() : &quot;&quot;) +
((path) ? &quot;; path=&quot; + path : &quot;&quot;) +
((domain) ? &quot;; domain=&quot; + domain : &quot;&quot;) +
((secure) ? &quot;; secure&quot; : &quot;&quot;);
document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
var dc = document.cookie;
var prefix = name + &quot;=&quot;;
var begin = dc.indexOf(&quot;; &quot; + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(&quot;;&quot;, begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}


</script>

<body onLoad='popup()'>


... Hope that helps. Sorry again for the late reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top