Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do this bit...

Status
Not open for further replies.

tsisher

Programmer
Sep 14, 2004
36
GB
Is there any way I can capture form data if user closes the browser ?

I have got a form where a user fills the form fields but never hits the 'Submit' button and closes the browser. I still want to capture the form data AND SAVE IT INTO THE DATABASE.

This is required for some sort of history about user's information. I am wondering if there is any way to do that..

Thanks a lot.

 
use this function
Code:
window.onunload = check_Browser_close;
var isClosing = false;
function check_Browser_close(){
	if (!isClosing){
		myWin=window.open('saveBeforeClose.asp?saveInfo='+mystr,....)
	}
}
insert the data in to the database in saveBeforeClose.asp
and close the browser in the onload of saveBeforeClose.asp

hope this helps

 
Thanks for your help. Really appreciate this.

I am bit confused..
where I am gonna write this line..
------------
window.onunload = check_Browser_close;
-------------

 
That line would go here:

Code:
<html>
 <head>
   <script language="javascript">
   <!--
   window.onunload = check_Browser_close;
   var isClosing = false;
   function check_Browser_close(){
     if (!isClosing){
         myWin=window.open('saveBeforeClose.asp?saveInfo='+mystr,....)
     }
   }
   -->
   </script>
 </head>
 <body>
  ...
 </body>
</html>

Additionally, you could do it like this:

Code:
<html>
 <head>
   <script language="javascript">
   <!--
   var isClosing = false;
   function check_Browser_close(){
     if (!isClosing){
         myWin=window.open('saveBeforeClose.asp?saveInfo='+mystr,....)
     }
   }
   -->
   </script>
 </head>
 <body onunload="check_Browser_close();">
  ...
 </body>
</html>

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 

I am trying to just open the new page on unload event but this script is not opening it..
This is what I am trying to do..

<script language="javascript">
<!--
window.onunload = check_Browser_close;
var isClosing = false;
function check_Browser_close(){
if (!isClosing){
myWin=window.open('saveBeforeClose.aspx');
}
}
//-->
</script>

Any idea why ????
Right now I am not even passing the parameters..
 
Are you using FireFox? They block popups like this. These are the popups that are considered "annoying" to popup blockers, as they are not triggered by a user's actions.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
No No. I am not using FireFox.. But I have got Google and Yahoo pop blocker but at the moment I have set them to popup allowed..

 
No No. I am not using FireFox.. But I have got Google and Yahoo pop blocker but at the moment I have set them to popup allowed..
 
This worked for me. Remove the "window." from the statement:

Code:
<script language="javascript" type="text/javascript">
<!--
onunload = doSomething;

function doSomething() {
	window.open('', 'win', '');
}
-->
</script>

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
No Luck mate..

But in visual studio.net if i right click the page say view in browser it open the page in visual studio.net and on closing it, it opens another blank page.

But if type it in browser -- it doesn't work at all.

very weird ..
 
Well the code I pasted above should open a blank page. Not sure why it's not working. What do you mean "if I type it in the browser"?

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
rsisher first time when you load the page this function will not be called ,i think you know this

this function will get called when you refresh the page or when you close the browser even then also some where you have to set the value isClosing = true depends on your requirement..

let me know if you still getting problem..
 
I know first time this function will not be called. But it should be called when I close the browser by pressing cross.

or as you said by setting the isClosing = true.. BUT IT'S NOT OPENING THE NEW PAGE.

Another intresting thing If I put an alert before opening the page it gives me an alert but never opens the page..

Does this to do with my browser? It's I.E 6.0
 
i think nothing to do with IE6.0
script language="javascript">
<!--
window.onunload = check_Browser_close;
var isClosing=true;
function check_Browser_close(){
if (!isClosing){
myWin=window.open('saveBeforeClose.aspx');
}
}
//-->
</script>
<BODY onload="Javascript:isClosing = false;">

try this other way round..
 
are you using windows xp sp 2 by any chance?

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
mrpro --> I tried that way also but no luck..
cLFaVa--> using windows xp sp 1

I know I am taking your time for this weird thing. I am sorry guys.
 
as far as i see there is nothing to prevent running the code

check once again

is there any pop up blocker (google)as cLFlaVA said

and give us if is there any other code you are running in that page..

 
All the web pages are .aspx pages. But for the time being there is no code behind only these javascripts.

I do have google pop up blocker as well as yahoo but are set to pop up allowed.

I have a felling that this is some thing to do with the browser. Because every thing works fine if I run the page in visual studio.net by right clicking the page and view in browser and it open ups the page in visual studio.net browser and on closing the browser it open up another page
as what we expect.. but when type the url in address bar of the explorer it does not works..
 
And what browser does vs.net open it with? i would suggest not even using that functionality from a developmental perspective. unless it opens in an actual browser, the browser they provide is likely not worth even looking at.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top