Session cookie (cookie with no expire date) or sessionStorage (HTML5). Basically, the first time a user shows up on that page, display the pop up (modals are less annoying, and can't be blocked by pop up blockers) and set the value of a session cookie or sessionStorage item to true. On every page load, check to see if it exists; if it does, do nothing, it already popped; if it does not, pop it up.
And don't use document.write or .writeln. Ancient, deprecated, and cannot be used after a page loads.
__________________ ^_^
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
But it's working in every browser? (IE 6-10, Firefox, Safari, Chrome etc.)
I'm not sure I understand your use of the words "ancient, deprecated, and cannot be used" if I'm using it and it's working. What do you mean?
document.write() has in most situations been obsolete since Netscape 3 passed away 10+ years ago. That is because document.write() statements must be run before the page finishes loading.
Wolfshade is saying that any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.
Wolfshade is implying that you should use DOM methods to load a .js file dynamically. However, in this situation document.write() will still work fine as your are running it before the page finishes loading. Still not regarded as very best or most modern practice, though.
Here is a suitable cookie script:-
Code:
<html>
<head>
</head>
<body>
<script type="text/javascript">
// PLACE THIS SCRIPT RIGHT AHEAD OF THE CLOSING </BODY> TAG
function createCookie(name,value) {
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);
}
if (!readCookie('wroteIt')){
document.write('Your Message Goes Here And You See It Only Once Per Browser Session');
createCookie('wroteIt', 'wroteIt');
}
//eraseCookie('wroteIt'); // FOR TESTING PURPOSES
</script>
</body>
</html>
Instead of the message you can place your <script> tag.
All modern browsers support document.getElementById. document.all went out with IE4.
The cookie remains active until the browser is closed, that is for the duration of the browser session.
“A man ceases to be a beginner in any given science and becomes a master in that science when he has
learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".