10

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");
}
2
  • 2
    You are forgetting that web is a client/server model. The way you are approaching this is impossible. It is a simple case of chicken and egg, cart before the horse etc. The updated value in the hidden field will not be available until the form is posted back. Commented May 20, 2009 at 3:00
  • ha ha..I am not forgetting what I know. I want some suggestion to get javascript status from serverside. I think there is some way which I could not think at this moment. Commented May 20, 2009 at 5:59

4 Answers 4

13

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. The html that rendrered after using your 2nd option <noscript> <input name="MyDetail$test" type="hidden" id="MyDetail_test" value="1" /> </noscript> <script type="text/javascript"> <!-- document.write('<input name="MyDetail$test1" type="hidden" id="MyDetail_test1" value="0" />'); //--> </script> <input id="MyDetail_test1" type="hidden" value="0" name="MyDetail$test1"/> Actually I want to get the value from server side. If I add runat="server", both hidden field can be retrived if js enabled. What do u suggest?
javascript runs on the client AFTER the page on the server. Like I mentioned in a comment on your original question, what you're trying to do is not possible. In order to read the new value of the field, a postback must occur. There isn't really a connection between the browser and the page.
Thanks Oisin for your help and suggestion. I agree with you.
3

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.

2 Comments

Thanks Dredel. Actually I have a grid with a 'Go' button. I wanted to redirect the corresponding js/nonjs page with dynamic querystring at grid_ItemCommand method.That's why I needed to know browser status from code behind. Now It seems to me that I have to redirect it somehow from javascript as you suggested
keep in mind that "redirection" can be done a couple of ways without javascript. You can set a hidden form to "submit()" to the next location, when the user presses your "go" button, you can make the go button a link (passing params along with the location change) and you can do the old Meta redirect as the page is loading. If you offer more details on what exactly you're doing, I can give you a less generalized piece of advice :)
2

javascript can set cookie by itself.

var myDate = new Date()
document.cookie = 'IJE=1; expires=' + myDate.toGMTString()

Does this resolve your problem?

2 Comments

Thanks for your help. I had done the same thing so that I dont need to set it from code behind. But if someone enables javascript after that..the cookie exists. I have to delete/ set IJE=0 from server side.
You can always set IJE=0 from server-side, and then set IJE=1 by javascript :)
1

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.

1 Comment

Thanks Daniel for your suggestion. 'Unobtrusive JavaScript' is a elegant way to manage javascript. Thanks for sharing. Actually according to project's requirement I need to detect browser's Javascript and save somewhere for further task in server side. Thats why I was looking for a solution that may be overlooked by me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.