Reputation: 471
I am using a hidden field 'Isjsenabled' to detect whether Client's Javascript is enabled or disabled. If Javascript is enabled, Javascript function will be fired and it will set hidden field's value to 1.
I want to check the hidden value from server side Page_load. But the problem is Javascript gets fired after Page load.
Do you have any suggestion ?
Html Part
<script type="text/javascript">
$(function() {
$("#<%= Isjsenabled.ClientID %>").val(1);
});
</script>
<input id="Isjsenabled" runat="server" value ="0" type="hidden" />
Code Behind Part
protected void Page_Load(object sender, EventArgs e) {
HtmlInputHidden hdn = this.FindControl("Isjsenabled") as HtmlInputHidden;
if (hdn != null)
if (hdn.Value != null && hdn.Value == "0")
Helper.SetCookie("IJE", "0");
else
Helper.SetCookie("IJE", "1");
}
Upvotes: 10
Views: 26047
Reputation: 28419
you should draw your script or noscript header as a page onto itself that then redirects (either through a meta tag or through a script location.href) to the next place you want to go based on if javascript is turned on. It'll take 2 steps, but it'll get you the result you're looking for. You can pass the information you need back to yourself in the query of the redirect.
Upvotes: 3
Reputation: 1521
javascript can set cookie by itself.
var myDate = new Date()
document.cookie = 'IJE=1; expires=' + myDate.toGMTString()
Does this resolve your problem?
Upvotes: 2
Reputation: 15070
Unobtrusive JavaScript may be of interest. Allowing your pages to degrade gracefully will simplify development. Design for no JavaScript initially and enhance the experience. This may not be an option if your application is extremely JavaScript intensive and absolutely requires it to function. In that case using the noscript tag to inform your users that they need JavaScript enabled would be a good idea.
Also, look at x0n's comments. Here a reference to help understand the HTTP request life cycle.
Upvotes: 1
Reputation: 52480
Without thinking too hard about the direction you're trying to go in, there is a little used tag that is supported by all browsers called NOSCRIPT.
<noscript>
<img src="http://myserver/notify_no_script.aspx" style="display:none">
</noscript>
As you can see, the idea is to make the browser request an image but only if javascript is disabled. This page could set a session variable stating that the current client has no script capability that would be available to all code in the current session.
Or, you could follow your current methodology:
<noscript>
<input type="hidden" name="noscript" value="1">
</noscript>
<script><!--
document.writeline('<input type="hidden" name="noscript" value="0">');
//--></script>
Hope this helps,
-Oisin
Upvotes: 13