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 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    36
    Thanks
    6
    Thanked 0 Times in 0 Posts

    refresh detection algorithm not working

    Hello,

    I'm trying to write some javascript that will detect if the page has been loaded because of the refresh button being pressed. I've searched google on how to do this, and several websites recommend something similar to the code I'm implementing below:

    Code:
    <script type="text/javascript" language="javascript">
    
    // in head
    
        function checkRefresh() {
    
            alert("BEFORE: value = " + document.getElementById("visited").getAttribute("value"));
    
            if (document.getElementById("visited").getAttribute("value") == null ||
                document.getElementById("visited").getAttribute("value") == "")
            {
                document.getElementById("visited").setAttribute("value", "refreshed")
            }
    
    
            alert("AFTER: value = " + document.getElementById("visited").getAttribute("value"));
        }
    
    </script>
    
    ...
    
    <body onload="Javascript:checkRefresh();">
    
    ...
    
        <form id="hiddenform">
        <input type="hidden" id="visited" value="" />
        </form>
    
    </body>
    My variation differs from most of the examples on the internet in a few ways (which may or may not affect its functionality):

    1) Most examples access the elements by directly using their names (as in: document.hiddenForm.visited.value). I'm using document.getElementById(...).getAttribute(...) just because that seems to be the safest way to ensure you are in fact getting the elements you want, and setAttribute(...) to ensure you're setting the attribute in the proper way. This entails that I need to set the ID in the form and input elements rather than the name.

    2) I'm accessing the input tag directly (rather than going through the form) because I really don't see how this would make a difference.

    3) I'm doing all this within asp:content tags which, from what I understand, can affect the behavior of the elements within it.

    I'm not sure if this works out for other programmers, but for me it doesn't seem to be working. My alert messages in the checkRefresh function tell me that the value of the input element does indeed change as expected, but it seems to get wiped out and reinitialized to the original value of "" when the page is refreshed.

    Am I doing something wrong?

    Thanks for any help.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    this seems to do more what you are expecting:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    window.onload=checkRefresh;
        function checkRefresh() {
            alert("BEFORE: value = " + document.getElementById("visited").value);
    
            if (document.getElementById("visited").value == null ||
                document.getElementById("visited").value == "")
            {
                document.getElementById("visited").value="refreshed"
            }
            alert("AFTER: value = " + document.getElementById("visited").value);
        }
    
    </script>
    </head>
    <body>
        <form id="hiddenform">
        <input type="hidden" id="visited" value="" />
        </form>
    </body>
    </html>
    although a cache-clearing refresh (ctrl-F5 in most browsers) gives the same result as a first-view page

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    36
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho
    although a cache-clearing refresh (ctrl-F5 in most browsers) gives the same result as a first-view page
    That may be it, but I still get the same result after turning caching back on (in the browser, in the ASPX page, and on the server).

    Any other suggestions?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Refreshing a page doesn't actually reload the page and so does not trigger the onload handler again.

    Reloading a page will clear any existing scripts and reload them so there'd be no way to assign a different value on reload to what was put there the first time (unless you use a cookie or localStorage/sessionStorage to keep track of the value between loads.

    Not all currently popular browsers support getAttribute/setAttribute so accessing the attributes directly is currently the safest option.
    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.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    36
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks everyone but I'm handling this with C# codebehind.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by gib65 View Post
    Thanks everyone but I'm handling this with C# codebehind.
    How do you plan on getting people to download that code and install it in their browser then? You can't use server side code to test for page refresh because the request never gets to the server - the page gets refreshed from the browser cache on the same computer as the browser is on.

    That's probably why it isn't working - the browser refresh never sends anything to the server to run your code.
    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.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    I don't know anything about C# codebehind, but this seems to be a cookie-free way around the reload/refresh problem:

    Code:
    <html>
    <head>
    <title></title>
    </head>
    <script  type="text/javascript">
    switch (window.location.hash){
    case "#refresh":
    alert("refreshed the page, didn't we?")
    break;
    default:
    alert("first time, huh?")
    window.location.hash = "#refresh"
    break;
    }
    </script>
    <body>
    </body>
    </html>


  •  

    Posting Permissions

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