简体   繁体   中英

Asp.Net - Redirect if JavaScript is not enabled

I got this code in my Master page :

<script type="text/javascript">
    $("#<%=hfJavaScriptDetected.ClientID %>").val('yes');
 </script>

<asp:HiddenField ID="hfJavaScriptDetected" runat="server" Value="no" />

So if JavaScript is enabled the value of my hidden field should have be changed.

Now, what I would like to do is check this value on server side and if it's set to "no", I want to redirect the user to the page Javascript.Aspx.

I don't know in which event to look the hidden field value. I try on the Page_Load event but it's seems the hidden field value was not already set.

would this not be easier like this?

<noscript>
  <meta http-equiv="refresh" content="1;URL=http://www.mysite.com/Javascript.Aspx"/> 
</noscript>

If the browser has javascript enabled, the inner content is ignored... but if they don't the meta tag says, in 1 second, refresh this page... and the new URL is ... Javascript.Aspx

The reason it is not set is because $("#<%=hfJavaScriptDetected.ClientID %>") is null. You need to wait for the page to load before you can set values.

What you want is:

$(document).ready(function() {
     $("#<%=hfJavaScriptDetected.ClientID %>").val('yes');
});

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