views:

70

answers:

4

I have the following code in my aspx page in which Javascript is disbaled will appear only if javascript is disabled from browser

<div id="jsDiv" runat="server">
    <noscript>
       Javascript is disbaled 
    </noscript>
</div>

in C# code if i get the innerText or innerHtml of jsDiv at any time it will get the same result of div content either javascript is enabled or even disabled ...
my question is : How can I detect the current innerText of the div that appear to user in the browser from C# code ?

A: 

but if javascript is disabled, you probably can't use innerText, etc...right?

PeterWong
it's a property already exist to HtmlGenericControl and can be used any time in C#
Hotmoil
oh sorry for misunderstanding. But just like @Larry said, you won't know the browser's state when you are in server side and that's make sense.Ask if you still can't get the way to do it^^
PeterWong
A: 

To detect if Javascript is enabled or not, you'll just have to get JS to alter some request in some way to indicate that it is present.

For example, you could rewrite the href of some links, or fill in a form input value, or (probably the best option) send an AJAX request back to the server. The server should then store that this particular session is JS enabled and take the necessary actions for future requests.

nickf
Could you suppoert me with an example , please ?
Hotmoil
A: 

You can't use a Server Control to detect if the browser will have Javascript turned on or not for the current page. Reason: you're trying to predict the future since the page has not yet been sent to the client!

Use the techniques of nick's answer to detect JS, then store the answer in the user's session.

Added: See http://www.codeproject.com/KB/user-controls/CheckJavascriptEnabled.aspx

Larry K
A: 

index.html

<html>
  <head>
    <!-- Redirect in 10 seconds -->
    <meta http-equiv="refresh" content="10;url=http://localhost/Default.aspx" />
    <script>
      <!-- Redirect to same page, with querystring js = true -->
      window.location="http://localhost/Default.aspx?js=true";
    </script>
  </body>
    Redirecting to new page <noscript>that does not need javascript</noscript>
  </body>
</html>

In your Default asp.net page, you can check Request.QueryString["js"] for javascript support or not.

Holystream