SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Form submitted intermittently upon window close in IE

    I have code in place to catch the window close event. At window close time I want to submit a hidden form that I update during the session. The updating is working flawlessly. The call to submit() within onbeforeclose() works on most browsers except, of course, IE running on 2000/XP. Sometimes IE makes the form submission before closing the window and sometimes not. If I add an alert message after the call to the submit() method (see code below), IE always makes the form submission. It is almost as if the form object is sometimes destroyed before it can be submitted.

    Has anyone ever run into this problem? Using a modal alert message to pause the window close long enough to submit the form is not an option available to me. Is there a way to verify that the form has been submitted before allowing the window close operation to continue?

    code follows:

    HTML Code:
    <script language="JavaScript">
    <!--   
    
    window.onbeforeunload = unload_handler;
    
    function unload_handler()
    {
        var frm = document.getElementById("FORM1");
    
        frm.KEY.value  = "key value";
        frm.DATA.value = "data value";
    
        frm.action = "accessCustomData.jrun?ACTION=storeQuiet";
        frm.submit();
    
    }
    
    //-->
    </script>
    
    <form id="FORM1" name="FORM1" method="post" style="height:0px;display:none;" target="invisibleFrame">
        <input type="hidden" name="KEY" value="">
        <input type="hidden" name="DATA" value="">
    </form>
    Last edited by pdppdpdd; Mar 9, 2006 at 08:24.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you using onbeforeunload? You aren't trying to cancel the unload event, and according to ppk:
    its use is strongly deprecated.
    Try onunload instead.

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is ppk?

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A well known js coder whose website is www.quirksmode.com. Lots of good info there.

    Does onunload work for you?

  5. #5
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    onunload works on some browsers but onbeforeunload works on more. At least, of the browsers we support.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Rancho Cordova
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.codingforums.com/showthread.php?t=81522

    Here is an example:

    Code:
    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    	<head>
    		<title>Enegam.com</title>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<script type="text/javascript">
    			window.onload=function()
    			{
    				setInterval("subForm",5000);
    			}
    			
    			function buildQuery2(form) 
    			{
    			  var a=[],i,o,ignored=/button|image|reset/i,toggled=/checkbox|radio/i;
    			  for(i=0;(o=form.elements[i]);i++){
    			    if(o.disabled||!o.name||ignored.test(o.type))continue;
    			    switch(o.tagName.toLowerCase()){
    			      case "input":
    			        if(toggled.test(o.type)){
    			          if(o.checked)
    			          a[a.length] = [o.name, o.value||"on"];
    			        } else if(o.value)
    			          a[a.length] = [o.name, o.value];
    			        break;
    			      case "select":
    			        if(!o.options.length)continue;
    			        var c=o.options[o.selectedIndex],v=[];
    			        if(!o.multiple)v=[c.value||c.text];else {
    			          for(var j=0;(c=o.options[j]);j++)
    			            if(c.selected) // optional: && !c.disabled
    			              v[v.length]=(c.value||c.text);
    			        }
    			        if(v.length)a[a.length] = [o.name, v.join(', ')];
    			        break;
    			      default:
    			        if(o.value)a[a.length] = [o.name, o.value];
    			    }
    			  }
    			  for(i=0;i<a.length;i++)
    			    a[i] = encodeURIComponent(a[i][0]) +"="+ encodeURIComponent(a[i][1]);
    			  return a.join("&");
    			}
    			
    			function getReqObj(url,func)
    			{
    				if (window.XMLHttpRequest)
    			  	{
    			 		xmlhttp=new XMLHttpRequest();
    			 	}
    			 	else if (window.ActiveXObject)
    			    {
    			    	if(new ActiveXObject("Microsoft.XMLHTTP"))
    			    	{
    			    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    			    	}
    			    	else
    			    	{
    			    		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    			    	}
    			    }
    			  	xmlhttp.onreadyStateChange=function()
    			  	{
    			  		if(xmlhttp.readyState==4)
    					{						
    						if(xmlhttp.status==200)
    						{
    							if(document.getElementById('nextPage'))
    							{
    								document.getElementById('nextPage').style.display='inline';
    							}
    							var last_mod=xmlhttp.getResponseHeader("Last-Modified");
    							var req=xmlhttp.responseXML;
    							var req_str=xmlhttp.responseText;
    							eval(func);
    						}
    						else
    						{
    							noData(xmlhttp.status);	
    						}
    					}
    			  	}
    				var now=new Date();
    				
    				if(url.indexOf("?")==-1)
    				{
    					var cut=url.substring(0,url.length);
    					var s=cut+"?c_date="+now;
    				}
    				else
    				{
    					var cut=url.split('?');
    					var s=cut[0]+"?c_date="+now+"&"+cut[1];
    				}
    				xmlhttp.open("GET",url,true);
    				var new_win=window.open(s,'new_win')
    				xmlhttp.send(null);
    			}
    			function subForm()
    			{
    				getReqObj('yourSubmissionPage.asp?'+buildQuery2(document.forms['hid_form']),'finishUpdate()');
    			}
    			function finishUpdate(req)
    			{
    				alert("You just submitted some info")
    			}
    		</script>
    	</head>
    	<body>
    		<form name="hid_form">
    			<input type="hidden" name="name1" />
    			<input type="hidden" name="name2" />
    			<input type="hidden" name="name3" />
    			<input type="hidden" name="name4" />
    		</form>
    	</body>
    </html>
    Basscyst

  7. #7
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    A well known js coder whose website is www.quirksmode.com. Lots of good info there.
    You mean www.quirksmode.org.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •