homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
How do you stop a javascript showing on refresh/browser back?
internetheaven

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4552716 posted 9:49 pm on Mar 8, 2013 (gmt 0)

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

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4552716 posted 9:59 pm on Mar 8, 2013 (gmt 0)

You'd probably want to write to a cookie when the form is shown the first time, then check that cookie to see if it's been shown.

internetheaven

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4552716 posted 9:41 am on Mar 12, 2013 (gmt 0)

Okay, well I have this script:

<script type="text/javascript">

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('oneOff')){
document.write('My message is here.');
createCookie('oneOff', 'oneOff');
}

</script>


but it won't work in IE9? Only Chrome, Firefox, Safari and IE6 ... any ideas?

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4552716 posted 10:43 am on Mar 12, 2013 (gmt 0)

have you checked your Privacy and Security settings in IE9's Internet Options?

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4552716 posted 1:23 pm on Mar 12, 2013 (gmt 0)

I'm not sure. I don't have IE9 available at the moment, but it works in IE8. Here's a complete test I put together quickly (note, the document.write version worked too, but I modified it to use jQuery's append method instead):

<!doctype html>
<html>
<head>
<title>Cookie Example</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
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);
}

// The first time through, this will display 'My message is here'.
// The second time, it will display 'oneOff'.
var cookieValue = readCookie('oneOff');

if (!cookieValue){
$(document.body).append(
$('<div></div>', {
text: 'The cookie was not set. Setting it now to "Hello World"'
})
);
createCookie('oneOff', 'Hello World');
}
else {
// The cookie was set
$(document.body).append(
$('<div></div>', {
text: cookieValue
})
);
}

$(document.body).append(
$("<input>", {
type: 'button',
value: 'Erase Cookies'
})
.on('click', function() {
eraseCookie('oneOff');
alert('Nom Nom Nom! Cookie eaten! Refresh the page to test.');
})
);
</script>
</body>
</html>

internetheaven

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4552716 posted 8:08 pm on Mar 12, 2013 (gmt 0)

Thank you very much for your help and I may try that more modern version.

As for the original, I found the problem ... the doctype needed to be changed from html to xhtml.

Seriously ... six hours of trying everything else and THAT was the problem? I don't even know why. It was just another random-fingers-crossed change that seemed to work!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved