Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
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
 
Old September 2nd, 2010, 03:20 AM
Authorized User
 
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Cade_Feng
Default 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
 
Old September 2nd, 2010, 03:26 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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?
__________________
Joe
http://joe.fawcett.name/
 
Old September 7th, 2010, 03:09 AM
Authorized User
 
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Cade_Feng
Smile

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
 
Old September 7th, 2010, 10:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The second technique (session cookies) in this article shows one way of doing it: http://www.tedpavlic.com/post_detect...javascript.php
__________________
Joe
http://joe.fawcett.name/
 
Old September 7th, 2010, 10:41 AM
Authorized User
 
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Cade_Feng
Thumbs up

Thanks,This is work. But How to judge this page is closed?
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
 
Old September 7th, 2010, 10:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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?
__________________
Joe
http://joe.fawcett.name/
 
Old September 7th, 2010, 09:04 PM
Authorized User
 
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Cade_Feng
Smile

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
 
Old September 8th, 2010, 03:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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.
__________________
Joe
http://joe.fawcett.name/
 
Old September 8th, 2010, 01:21 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

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.
 
Old September 8th, 2010, 09:19 PM
Authorized User
 
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Cade_Feng
Default

Finally,I know something. Thanks for helping me.
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pop-Up Close / Refresh Parent cmiller HTML Code Clinic 7 October 6th, 2011 05:43 AM
how to include .js file in .aspx page shanwaj ASP.NET 2.0 Basics 13 March 11th, 2008 07:36 AM
Remote Scripting(ASP) popup(js)refresh after sent icecoolcorey Pro Java 3 March 27th, 2007 12:19 AM
passing parameter from js to another jsp page. Regornil JSP Basics 1 August 31st, 2004 08:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.