File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Preventing form submission twice on doubleclick in NN6. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Preventing form submission twice on doubleclick in NN6." Watch "Preventing form submission twice on doubleclick in NN6." New topic
Author

Preventing form submission twice on doubleclick in NN6.

Gun Mali
Greenhorn

Joined: Feb 16, 2004
Posts: 4
Hi,
I have a form in which I have this link :- <a href="javascript:submitform(parameters );"> . In the JavaScript function submitform, there is the code for submitting the form.This works fine with single click on both IE,NN6.In IE the form is submitted only once even for double click.But if you double click on the link in NN6,the form is submitted twice. This causes problem. The workaround for this was that we set a flag(indicating form submission) in the submitform function, we also set a timer which calls the function resetflag after 3 seconds.So any click within 3 seconds of the first click will be ignored (so that the form is not submitted for the second click).
function submitform(params)
{
if(flag==0)
{
flag=1;
timeout = setTimeout('resetflag()',3000);
document.form1.action = someURL;
document.form1.submit();
}

}
function resetflag()
{
flag=0;
}
.
.
.
.
.
.
.
//Global Javascript variables initialization.Executed while page loading.
var flag=0;
var timeout=0;
But the problem now is that, the event fired on the first click submits the form to the server.The second click is ignored since the flag is 0.But the results to be showed for the first click event fired are not shown.The page just stands still as if no event was fired.I dont want to show any alerts or message like 'Double click is not allowed'. I just want a situation where double click has the same effect as single clicki.e. form should be submitted only once(as in single click) and the results should be displayed. Can Anyone Help?
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
Just believe me - user could always srew something up. As far as I remember, the following things where done in such situation, but none of them is flawless:
1. Setting a flags
2. Disabling a link.
3. Showing a transparent DIV on top of whole page (page will became "readonly") for couple seconds. setTimeout should be used to remove this DIV, in case user clicks Stop button. - I like this one.
4. doing something like <A href='#' onclick='submitIt()' ondblclick='submitIt()'>
5. whole bunch of server side things - session and request flags, and so on
Gun Mali
Greenhorn

Joined: Feb 16, 2004
Posts: 4
Hey Yuriy, you're right.I have posted this problem in about 15 Javascript forums and the replies I got were exactly amongst the ones that you had listed. I have tried 1,2, and 4 of them but doesnt work.Since I have posted on "Javascript" forums no one suggested me option 5.I tried option 5 but that too doesnt work.
The reasons I suppose for they not working are :-
Option 1:-Flag setting
For the second click the condition if(flag==0) is not satisfied so form isnt submitted but there is no code for else part so nothing happens and the page just stops.Even the response for form submission for first click isnt displayed.In the else loop if I give return false,It displays me a completely blank page with the words "return false" on it.
Option 2:-Disabling a link
I did it with two ways.One was getElementById("spanid").innerHTML ="<a>Link that does nothing</a>".Second was document.links[id].disabled = true.Both dont work.May be bcoz the first click event is fired which starts to execute the submitform javascript function.After a fraction of a moment for the second click again the function is called and then we reach the code for disabling the link for the first click.But of no use coz we have already entered the function for the second click.Disabling the link now will only prevent further clicks.
In short,the events are fired so fast that they both are executed before the link getting disabled.
Option 4:-
<A href='submitIt()' onklick='submitIt()' ondblklick='submitIt()'>
Now some statistics.The events fired in NS and IE are as follows:-
(2 denotes that the corresponding event is fired twice)
--------------------------------------------------------------------------
Events defined |Event occurred |Events fired in IE |Events fired in NN6
--------------------------------------------------------------------------
clk,dblclk,href | clk | clk,href | clk,href
|---------------|-------------------|--------------------
| dblclk | clk,href,dblclk | clk2,href2,dblclk
---------------------------------------------------------------------------
clk,href | clk | clk,href | clk,href
|---------------|-------------------|---------------------
| dblclk | clk,href | clk2,href2
---------------------------------------------------------------------------
dblclk,href | clk | href | href
|---------------|-------------------|---------------------
| dblclk | href,dblclk | href2,dblclk
---------------------------------------------------------------------------
Only href | clk | href | href
|---------------|-------------------|---------------------
| dblclk | href | href2
---------------------------------------------------------------------------
The problem is the href2 that occurs everytime for dblclk in the NN6 column.So the event will be fired twice no matter what events are defined.So I have to go with only href and then having some way of preventing the event for second href.

Option 5:-
Session and request flags.
Tha main problem that occurs for multiple form submission in my case is that, once the form is submitted and the request is sent, the buisness logic requires accessing a session bean , getting data from it and setting in session and request.Now if u aceess the same bean twice for the same session, it is giving me some LockTimedOutException. So I need to have some mechanism for concurrency check.Too tough to implement.I need a simple solution(Possibly in Javascript).

I didnt get what exactly option 3 tries to do. Can u plz spend some of ur time in replying for this.Also u ended ur listing with so on.Can u tell what they are now that the ones in the list arent working.
Gun Mali
Greenhorn

Joined: Feb 16, 2004
Posts: 4
Sorry for the garbled table for option 4.Hope this one is displayed right:-
-------------------------------------------------------------------------
Events defined**|Event occurred*|Events fired in IE*|Events fired in NN6
-----------------|----------------|-----------------|--------------------
clk,dblclk,href***|*clk**********|*clk,href*******|*clk,href
----------------|---------------|-------------------|--------------------
clk,dblclk,href**|*dblclk*******|*clk,href,dblclk***|*clk2,href2,dblclk
----------------|---------------|-------------------|--------------------
clk,href*******|*clk*********|*clk,href*******|*clk,href
----------------|---------------|-------------------|--------------------
clk,href********|*dblclk*******|*clk,href*******|*clk2,href2
----------------|---------------|-------------------|--------------------
dblclk,href*****|*clk*********|*href***********|*href
----------------|---------------|-------------------|--------------------
dblclk,href*****|*dblclk*******|*href,dblclk*****|*href2,dblclk
-------------------------------------------------------------------------
Only href*******|*clk*********|*href***********|*href
----------------|---------------|-------------------|--------------------
Only href******|*dblclk*******|*href**********|*href2
-------------------------------------------------------------------------
[ February 19, 2004: Message edited by: Gun Mali ]
[ February 19, 2004: Message edited by: Gun Mali ]
[ February 19, 2004: Message edited by: Gun Mali ]
[ February 19, 2004: Message edited by: Gun Mali ]
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
Here is an example for option 3:

<script>
var _timeout = 10000;
var _el=null;
function disablePage(){
if(_el == null){
_el = document.createElement("DIV");
_el.style.position="absolute";
//in this case images/spacer.gif is transparent 1 pixel image
// _el.style.backgroundImage = 'url(images/spacer.gif)';
_el.style.width = "1";
_el.style.heigth = "1";
document.body.appendChild(_el);
}
_el.style.visibility = "visible";
_el.style.zIndex = "1000";
_el.style.left = '0';
_el.style.top = '0';
if(document.all){
_el.style.width = document.body.scrollWidth;
_el.style.height = document.body.scrollHeight;
}
else{
_el.style.width = document.width;
_el.style.height = document.height;
}
setTimeout("enablePage();",_timeout );
}
function enablePage(){
_el.style.visibility = "hidden";
_el.style.zIndex = "-1";
}
function doSubmit(){
disablePage();
document.myForm.submit();
}
</script>
<a href="javascript:doSubmit()">Submit a form</a>
<form name="myForm" ....>
.....
</form>
[ February 19, 2004: Message edited by: Yuriy Fuksenko ]
Gun Mali
Greenhorn

Joined: Feb 16, 2004
Posts: 4
Hi Yuriy,
Thanx for giving ur time for me.I got the solution of the problem.
Need to replace:-
<a href="javascript:submitform(parameters );">
With this:-
<a href="#" onklick="javascript:submitform(parameters );">
I was using flags but I was calling the submitform function with href and not for the onclick event,so the page was just stopping.As per this change I called it with onclick event and it did exactly what I wanted.It let the first click pass and stopped the second one. Also the page refreshed after receiving response for the first click.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preventing form submission twice on doubleclick in NN6.