PDA

View Full Version : Javascript code to submit the page does not work sometimes in IE



SantoshKS
02 Dec 2010, 12:25 AM
Hi,

I have a jsp which has a function gets called during the page unload.
This jsp has a form with an action attribute to one servlet.
Please find below the code snippet.

---- Java script code ---
function logoff() {
document.forms["FormName"].target="_self";
alert('Submitting the form');
document.forms["FormName"].submit();
}

--- HTML form code -----
<body onUnload='logOff()'>
<form name ="FormName" method="POST" action='<%=sLogoutResponseURL %>'>

Whenever the page gets unloaded always logoff function is getting called. I also get the alert message "Submitting the form". But my servrlet does not get invoked. My application supports logging in with the same user id more than once.

Scenario it works: User logs in. Close the application. logoff function is called and servlet gets invoked.

Scenario it does not work: User logs in one window. Open one more IE window and and open the application with same user id. this can be repeated. Now when i close the application logoff function is called for all of the window close. But servlet does not gets invoked all the time. Sometimes it stops calling servlet.

I have searched over the net and found one solution.
Below piece of code will make sure form gets always submitted.
-------------------------Start -----------------
function fakeSubmit(event) {
var target = event ? event.target : this;
/* Fire a submit event */
var fakeEvent = $(target).fire("form:submit");
if (fakeEvent.stopped == false) {
/* Call the real submit function */
this._submit();
}
}

if(window.HTMLElement){
/* DOM-compliant Browsers */
HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = fakeSubmit;
} else {
/* IE does not expose it's HTMLElement object or its children
Let the document load so all forms are accounted for */
document.observe("dom:loaded", function(){
for(var i = 0; i < document.forms.length; i ){
document.forms[i]._submit = document.forms[i].submit;
document.forms[i].submit = fakeSubmit;
}
});
}
-----------End ------------------

But this resulted in script error while loading the page "object does not support this method or property" at line where document.observe is there.

Kindly help at the earliest.

Thanks in advance
Santosh