/    Sign up×
Community /Pin to ProfileBookmark

differentiating between reloading and leaving a page

I need some help on determining whether the browser is coming into a webpage from a different page or merely reloading the same webpage. The reason is the following:

I need to have a cookie to indicate whether or not to fill in today’s date in a textbox on my webpage. A checkbox toggles the value of the cookie; by default, this cookie is set to true (as in the checkbox will be checked). I only want the date to be filled in when I enter the page. If I navigate to the page, the date should be filled in. If I change the date in the textbox and reload the page or navigate to the same URL, that date should not be overwritten with today’s date. If I leave the page and come back, today’s date should be filled in.

My plan was to check the cookie and when the onload event is triggered, but then when I reload the page the date keeps getting overwritten. Is there an easy way to solve this problem? Thanks in advance for any help.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 30.2006 — If your cookie is a boolean, how does the browser know what the user typed?.

Why not set the cookie to the value of the text element?
Copy linkTweet thisAlerts:
@covetous_kidauthorMar 30.2006 — Regardless of what the value of the cookie is, it still doesn't solve the problem. If I store the date value in the cookie and simply reload the value of the cookie into the textbox, then I will never know whether or not I should input today's date or not. Say I changed the date to yesterday. If I leave the page and return, it will still show yesterday's date instead of today's date.
Copy linkTweet thisAlerts:
@mrhooMar 30.2006 — Sorry,covet- I thought you wanted to know if the date had been changed by the user, and to persist that value for subsequent visits to the page.

You always know, when the page loads, what today's date is. You know, from the value of the cookie, if the user changed the date. What's missing?
Copy linkTweet thisAlerts:
@A1ien51Mar 30.2006 — There is a check that sometimes works.

Have a input on the page that has nothing in it. When the page loads and it does not have a value then you know it is a clean entry. Now fill that input in with a value. Now if the page is refreshed the value tends to stick in the field and the onload would detect the value and know it was a refresh.

I would set a session cookie and basically say (I been here already). With that you would know they are there and I think that is what. A session cookie will be wiped clean when the user leaves and closes their browser.

Eric
Copy linkTweet thisAlerts:
@covetous_kidauthorMar 30.2006 — 

I would set a session cookie and basically say (I been here already). With that you would know they are there and I think that is what. A session cookie will be wiped clean when the user leaves and closes their browser.

Eric[/QUOTE]


Would this solution be able to determine if the page is linked from another page or refreshed? My concern is that, even if I set a session cookie, it could only say "I've been here before", and not "I'm coming right back". It would be so much easier if I could figure out in the onunload event what the target URL is. Is this possible?
Copy linkTweet thisAlerts:
@felgallMar 30.2006 — It would be so much easier if I could figure out in the onunload event what the target URL is. Is this possible?[/QUOTE]

No it is not possible to do that.

With a session cookie you can easily determine whether this is the first visit to the page in the current browser session or that the person has been on the page before (and either left or refreshed the page). If you use the session cookie to store the latest page visited on your site then you will be able to distinguish when the person is returning to the page from elsewhere on your site. The only things you wont be able to distinguish between is refreshing the page and leaving the page to go elsewhere on the web and then returning to the same page but since this last situation is unlikely unless the person uses the back button you could probably get away with treating that as a refresh as well.
Copy linkTweet thisAlerts:
@covetous_kidauthorMar 30.2006 — No it is not possible to do that.
[/QUOTE]


Oh bummer... I guess I'll have to stick to a session cookie for now then. Thanks for everyone's help. Just out of curiosity, is there another way to implement exactly what I want using something other than javascript?
×

Success!

Help @covetous_kid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...