/    Sign up×
Community /Pin to ProfileBookmark

Javascript code to submit the page does not work sometimes in IE

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@mityaDec 02.2010 — document.observe is a method of the Prototype JS framework, so if you're not using Prototype, it will be undefined. And calling remote scripts on unload is a tricky game because you're in a race against time for your request to complete before the browser has left the page and gone somewhere else.

You could return false from the unload event, which would stop the user navigating away from your page, but this is a) a bad idea; b) not reliable, since they could still close the browser/tab completely.

I would revise your methodology.
×

Success!

Help @SantoshKS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...