Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    120
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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!?!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder
    Join Date
    Apr 2010
    Posts
    120
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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!?!

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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)
    Last edited by Philip M; 03-09-2013 at 09:32 AM.

    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.

  • Users who have thanked Philip M for this post:

    instaunt (03-12-2013)

  • #6
    Regular Coder
    Join Date
    Apr 2010
    Posts
    120
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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!?!

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Apr 2010
    Posts
    120
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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!?!

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • #10
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •