 |
| Javascript General Javascript discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 2nd, 2010, 03:20 AM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How can i distinguish refresh or close page by js?
Hi, everybody ,
I wanna judge a page is Refresh or close by java script.
There's compatibility problem , who can save me!!!!!!!!
Thanks in advance!
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
|
|

September 2nd, 2010, 03:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
There's no silver bullet for this. In the past I've used cookies to record a page visit and then checked them on a subsequent visit.
What exactly is the problem you're facing?
|
|

September 7th, 2010, 03:09 AM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I want to judge client is close or refresh page at client side. Then I can execute
some statements; But I can't write proper java script for most browser. So.That is exactly my problem.
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
|
|

September 7th, 2010, 10:41 AM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks,This is work. But How to judge this page is closed?
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
|
|

September 7th, 2010, 10:56 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well you can use the onunload event. But it won't tel;l you if the user clicked the close or the refresh button, it's the same event. Perhaps you can say why you need to distinguish?
|
|

September 7th, 2010, 09:04 PM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yeah.Just like what I say. I need to execute some statements in there.
Such as, a Client log on a Bank page at the client side, but he log out doesn't through the exit button.So. We need do some thing to help him. Could u understand me?
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
|
|

September 8th, 2010, 03:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
So run some code in the onunload event. But that will still run if they refresh the page. A page refresh is a re-request of the previous URL, the HTTP protocol is fairly stateless and can't distinguish between you closing a page and then re-opening or refreshing it.
|
|

September 8th, 2010, 01:21 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
|
|
I agree, the problem like Joe said, is that HTTP doesn't remember one page request from the next. As far as the page and the browser is concerned, a refresh is no different from closing the page, because they BOTH close the page; a refresh simply navigates back to the same page, but it has no memory of ever being there. So if you want want to know if you've been here or not, you need to roll some custom Javascript to create that "memory" of being here in the page.
I would recommend writing a value to the cookie in javascript, you'd store a timestamp in the cookie with a key like "lastVisitedBankPage" when a user first navigates to the page. Then each time the page loads, check that key in the cookie and if it is set to a recent value, then you can assume they've refreshed the page. If the value is not there (they've never been to your site, or at least not since the last time they cleared their browser cache), or the value is old, you can assume they are coming back to the page for the first time.
I would NOT use the onunload method. This is the type of thing that spammers typically use to prevent someone from navigating away from their website, or to throw nasty pop ups around. That is a recipe form users who will have to turn off the computer and never visit your website again. Executing client-side scripting on unload is a major usability flaw and extremely rude into the bargain.
__________________
-------------------------
Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe
When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper
Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
|
|

September 8th, 2010, 09:19 PM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Finally,I know something. Thanks for helping me.
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
|
|
 |