Asp.Net - Redirect if JavaScript is not enabled

瘦欲@ 提交于 2019-12-14 03:08:45

问题


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.


回答1:


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




回答2:


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');
});


来源:https://stackoverflow.com/questions/1554688/asp-net-redirect-if-javascript-is-not-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!