Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2013, 09:49 PM   PM User | #1
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 91
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
How do you stop a javascript showing if the user clicks back to that page?

If you have a javascript that loads a popup form when the user lands on the site - how do you stop that form showing again on that page if:

a) the user refreshes the page.

b) the user follows a link on the page but clicks "back" on their browser.

I only want the form to load once. e.g.

<SCRIPT type=\"text/javascript\">
if (document.getElementById || document.all)
document.write('<script type="text/javascript" src="http://www.mysite.com/form.js"><\/script>')
</SCRIPT>

How do I make the stuff in the document.write area not happen/vanish on refresh or browser back?

Thanks
Mike
__________________
I've been making animations for my daughter. Tell me what you think!

Pickles and the Bully
Where are you Pickles!?!
instaunt is offline   Reply With Quote
Old 03-08-2013, 09:54 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 03-08-2013, 10:02 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Also don't use document.all - Internet Explorer 4 is long dead.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-08-2013, 11:13 PM   PM User | #4
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 91
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
Quote:
Originally Posted by WolfShade View Post
And don't use document.write or .writeln. Ancient, deprecated, and cannot be used after a page loads.
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?
__________________
I've been making animations for my daughter. Tell me what you think!

Pickles and the Bully
Where are you Pickles!?!
instaunt is offline   Reply With Quote
Old 03-09-2013, 07:54 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by instaunt View Post
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.

Last edited by Philip M; 03-09-2013 at 09:32 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
instaunt (03-12-2013)
Old 03-12-2013, 10:39 AM   PM User | #6
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 91
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
Thank you for that! It works great in Chrome, Safari and Firefox ...

... just won't work in Internet Explorer for some reason. I have IE6 and IE9 here. It only works in IE6.

Any ideas?
__________________
I've been making animations for my daughter. Tell me what you think!

Pickles and the Bully
Where are you Pickles!?!
instaunt is offline   Reply With Quote
Old 03-12-2013, 11:18 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
It works fine for me in IE9. Have you blocked cookies in that browser?
__________________

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.
Philip M is offline   Reply With Quote
Old 03-12-2013, 09:09 PM   PM User | #8
instaunt
New Coder

 
Join Date: Apr 2010
Posts: 91
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
After six hours of randomly changing things to see if it would help, I changed the doctype from html to xhtml ... and the script ran on IE9.

What the hell?!?! I don't even know why that would do it. It was just a random-fingers-crossed change. Six hours ...

... I wish I had gone to college and studied this stuff instead of learning it on the fly!
__________________
I've been making animations for my daughter. Tell me what you think!

Pickles and the Bully
Where are you Pickles!?!
instaunt is offline   Reply With Quote
Old 03-12-2013, 10:18 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
It works fine for me in IE9 with or without a !DOCTYPE.

There is no point in serving the page as XHTML.

You have somrthing in your browser setting which is creating the problem
__________________

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.
Philip M is offline   Reply With Quote
Old 03-12-2013, 10:20 PM   PM User | #10
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
I wonder if OP is using file:///C:\blahblahblah instead of http://localhost or http://domain.com.
__________________
^_^

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".
WolfShade is offline   Reply With Quote
Old 03-13-2013, 08:28 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by WolfShade View Post
I wonder if OP is using file:///C:\blahblahblah instead of http://localhost or http://domain.com.
It works just as well locally. You can easily test it yourself.
__________________

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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:13 AM.


Advertisement
Log in to turn off these ads.