By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Prevent Premature clicking on a submit button while page is refreshed

P: n/a
Scenario: You have a page that is TOO slow to refresh.
But it allows partial flushing of html contents. I.e.
Submit button already appears but you don't
want your users to click on it prematurely
because other parts are still coming.
Here I put a javascript the will enable only
submit button only after 5 seconds after the page
is load fully.

The prevention of button being clicked twice
is also done .
<html>
<head>
</head>
<body>

<form id="testForm" >
<input id="test" type=text>
<input type=button value="1click only" onclick="dosubmit()">
</form>

<script type="text/javascript">
<!--
var submitted = false;
var timer1=setTimeout('EnableClick()', 5000);

function EnableClick() {
if (typeof submitted == 'undefined') return false;
submitted = false;
}

function dosubmit(method) {
// prevent clicking twice
if (typeof submitted == 'undefined') return false;
if ( submitted ) return false;
submitted = true;
clearTimeout(timer1);
document.testForm.submit();
}
-->
</script>
</body

Aug 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
an******@hotmail.com wrote:
Scenario: You have a page that is TOO slow to refresh.
But it allows partial flushing of html contents. I.e.
Submit button already appears but you don't
want your users to click on it prematurely
because other parts are still coming.

How about using script to prevent submission say by disabling the submit
button. Then have an onload function that enables it.

Your method makes submission of the form dependent on JavaScript, which
it should not be, and makes users wait some arbitrary time that you
think might let the page load fully. Why 5 seconds? Some users will
have to wait longer than necessary, others will likely still not have
the form fully loaded.

Here I put a javascript the will enable only
submit button only after 5 seconds after the page
is load fully.
From the script below, you attempt to wait for 5 seconds after the
script is loaded, not when the page has finished loading. Your script
does not prevent submission anyway.

The prevention of button being clicked twice
is also done .
That should be handled at the server, not the client, and your script
does not guarantee only one submit.

<html>
<head>
</head>
<body>

<form id="testForm" >
<input id="test" type=text>
<input type=button value="1click only" onclick="dosubmit()">
</form>

<script type="text/javascript">
<!--
HTML comments in script elements are completely unnecessary and possibly
harmful, don't use them.
var submitted = false;
var timer1=setTimeout('EnableClick()', 5000);

function EnableClick() {
if (typeof submitted == 'undefined') return false;
'submitted' is defined as 'false', this test will always return false.
submitted = false;
}

function dosubmit(method) {
// prevent clicking twice
if (typeof submitted == 'undefined') return false;
'submitted' is never undefined, this test will always return false.
if ( submitted ) return false;
submitted = true;
clearTimeout(timer1);
document.testForm.submit();


This method of accessing form elements fails in Firefox, you need to
either give the form a name (testForm would be good) or use:

document.forms['testForm'].submit();

[...]

--
Rob
Aug 14 '05 #2

P: n/a
That is exactly the opinion I wanted! Great!

Aug 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.