简体   繁体   中英

How to redirect if JavaScript is disabled in ASP.NET mvc masterpage

我有一个error.aspx页,我想检查用户是否禁用了它的 JavaScript,然后重定向到一个简单的error.aspx页面

On Masterpage just add this code

<noscript>
<% Response.Redirect(Url.Action("ActionName","ControllerName")); %>
</noscript>

If user disabled the javascript it will redirect to specific controller action.

The usual way to approach this problem is to redirect a user who has javascript enabled and display the error for the user who has it disabled using the noscript tag.

 <script type="text/javascript">
     location.href = 'pagethatneedsjavascript.aspx';
 </script>
 <noscript>
     This page needs JavaScript enabled!
 </noscript>

Alternatively if your page isn't the first page the user would load in the current session, you could add a link to the page like

<a href="/linktopage.aspx?js=disabled"
onclick="location.href='/linktopage.aspx?js=enabled';return false;">the page</a>

If the user has javascript disabled, they'll go to the page referenced in the href attribute, if they have it enabled the JavaScript in the onclick attribute will be executed instead.

You can then on the server side read the querystring variable and redirect if it equals "disabled"

if ( Request.QueryString["js"] == "disabled" ) {
    Response.Redirect("error.aspx");
}

Note that if the page is bookmarkable the user might end up on the page using js=enabled without js actually being enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM