views:

3403

answers:

5

Hi,

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");
}
+10  A: 

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

x0n
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?
miti737
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.
x0n
Thanks Oisin for your help and suggestion. I agree with you.
miti737
+1  A: 

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.

Daniel X Moore
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.
miti737
+2  A: 

javascript can set cookie by itself.

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

Does this resolve your problem?

Sergey Kovalenko
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.
miti737
You can always set IJE=0 from server-side, and then set IJE=1 by javascript :)
Sergey Kovalenko
+3  A: 

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.

Dr.Dredel
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
miti737
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 :)
Dr.Dredel