When the user has javascript disabled, I would like to make the ajax.beginform not do anything at all, instead of submitting a regular full-page post request. What is a proper way to accomplish this?
1 Answer
No matter what you do if there's a form tag in the page and javascript disabled it can always be submitted. Even if you remove the action attribute it will submit to the same page. So you could use the <noscript> tag to provide an alternative markup to users with javascript disabled. Another possibility is to hide the submit button with CSS and show it with javascript (so that only users that have JS enabled can see it) but this is not ideal because the form could still be submitted by pressing Enter while typing inside an input.
I think a better way would be to leave the form submit a regular request and in your controller action handle this case and react accordingly:
if (Request.IsAjaxRequest())
{
// act as normal
return PartialView();
}
else
{
// the user had javascript disabled
return View();
}
2 Comments
span for the buttons instead of submit buttons and attaching click handlers unobtrusively to them using jQuery. So if the user has javascript disabled those click handlers are not attached and nothing happens because you are clicking on a simple span.