开发者

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

I have a masterpage on which I want to check if user has disabled its JavaScript then redirect to开发者_运维技巧 a simple error.aspx page


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜