views:

183

answers:

2

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  A: 

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');
});
Shawn Simon
I try it but when I'm in the Page_Load event the value is still set to 'no'
Melursus
try doing$("#<%=hfJavaScriptDetected.ClientID %>").attr("value", "yes");
Shawn Simon
I try and it's doesn't work. But I know the value is being change to 'yes' because if I do an alert, it's show 'yes'. I think the problem is when I try to see the value in the Page_Load event, this event have not already occurs ? So maybe I need to check in in another event ?
Melursus
oh yea, data population occurs after the page_load event. you could either use the onvaluechanged event of the hidden field, load complete, or look at the request.forms item collection. : )
Shawn Simon
I try your 3 solutions without success... I'm pretty new at playing with Javascript along with the server-side. My feeling is that because the value is change by client-side, the server-side doesn't have access to this new value ? I would really appreciate if you can try at your own the solutions you proposed and tell me if you are able to make one work. Thx!
Melursus
+5  A: 

would this not be easier like this?

<noscript>
  <meta http-equiv="refresh" content="1;URL=http://www.mysite.com/Javascript.Aspx"/&gt; 
</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

scunliffe
I liked this approach. The user need not have to submit the page to detect js is disabled or not.
Ramesh
Is there a negative side to use this method. It's always work ? Because yesterday I googled a lot to find a solution and I always end up with complicated script so I wonder why nobody have suggest this method ?
Melursus
@Melursus - I can't really think of one. I think most of the snippets out there are the reverse... see if JavaScript is available, then redirect. I presume in your case that JS is required for the site, thus you just want to redirect a user if they have turned it off. The meta tag should work fine for this. You can of course change the timer to 0 if you want instant... or longer if needed. The only catch I know, is that once a meta refresh tag has been read, you can't unset it in the browser. If you remove the tag (via JS), the browser won't unhook the request to refresh.
scunliffe