Post back to server when javascript is disabled [closed]
When javascript is disabled in browser then onclick button will post back to server.
Just use a normal type="submit"
input or button and <form>
combination (been around forever), like this:
<form action="mypage.htm" method="post">
//fields..
<input type="submit" value="Click to submit" />
</form>
In JavaScript, handle the submit
event of the <form>
...so you capture the submit, do something with JavaScript is enabled (AJAX submission for example), but it's not not enabled, it degrades gracefully and will submit to the server/do a full page refresh.
If you have the reverse problem that it's already like this and you don't want the submit behavior, then don't use a submit button...but I advise against this, since your site should degrade gracefully for JavaScript disabled users.
In case your "button" is part of a <form>
and that forms action is post
then yes, your form will gracefully degrade and do a normal post request to the server.
PS: Shouldn't a "Sr. Web Developer" know something like that?
<input type='submit' value='Submit' onclick='return doSomthing();' />
and then right a javascript function
function doSomething()
{
return false; // to prevent submit
}
The way to handle this is to make sure that the site works with pure HTML when JavaScript is disabled. Sometimes it means writing separate handlers for HTML form posts and Ajax form posts, sometimes you might be able to modify your code to work with both methods.
If you design the app JavaScript only, you can initially hide the JavaScript form or the button and restore it using JavaScript so that the HTML only user doesn't get to click it. You can use the noscript tag to inform the user to turn JS on, for example:
<form style="display:none;" id="ajaxForm">
(form content)
</form>
<script type="text/javascript">
$( '#ajaxForm' ).show(); // using jQuery here but can be done with pure JS
</script>
<noscript>
Sorry, but you need to enable JavaScript.
</noscript>
精彩评论