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 01-20-2012, 06:37 PM   PM User | #1
gib65
New Coder

 
Join Date: Nov 2011
Posts: 32
Thanks: 6
Thanked 0 Times in 0 Posts
gib65 is an unknown quantity at this point
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.
gib65 is offline   Reply With Quote
Old 01-20-2012, 06:53 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is offline   Reply With Quote
Old 01-20-2012, 08:29 PM   PM User | #3
gib65
New Coder

 
Join Date: Nov 2011
Posts: 32
Thanks: 6
Thanked 0 Times in 0 Posts
gib65 is an unknown quantity at this point
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?
gib65 is offline   Reply With Quote
Old 01-20-2012, 10:13 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-20-2012, 11:14 PM   PM User | #5
gib65
New Coder

 
Join Date: Nov 2011
Posts: 32
Thanks: 6
Thanked 0 Times in 0 Posts
gib65 is an unknown quantity at this point
Thanks everyone but I'm handling this with C# codebehind.
gib65 is offline   Reply With Quote
Old 01-21-2012, 10:06 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-30-2012, 07:56 PM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho 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 05:55 PM.


Advertisement
Log in to turn off these ads.