views:

48

answers:

2

Im placing no script tag on my master page that if a user dont have javascript enabled or its browsers doesnt support javascript it redirect to a specific view. im using this code

 <noscript>    
  <% Response.Redirect("../UserLogin/Error");  %>
 </noscript>

but the problem is its redirecting me everytime i open the page although my javascript is enabled. Im using asp.net mvc2 what may be the problem or is there is any other way to do this?

+3  A: 

Response.Redirect is executed on the server side and it will send a 302 status code to the browser which will automatically redirect. You probably want:

<noscript>    
    Please enable javascript to use this site.
</noscript>
Darin Dimitrov
+1 for brevity. I would add that a js-based redirect to the main site is also a possibility, though a bit of a hack...
Aatch
+1  A: 

The tag noscript is evaluated in the client (browser). The server doesn't know if client has script support, so every time it redirects.


EDIT

Explain better: the server just creates a string (HTML) and send it to browser. It doesn't parse to see if browser has script support.


EDIT 2

You can use meta refresh tag. See:

<head>
    <noscript>
    <meta http-equiv="refresh" content="0;url=http://example.com/" />
    <!-- Redirect to http://example.com/ immediately -->
    </noscript>
</head>

I tested in firefox and works.

Topera
You can't do that - "However, you can’t use `noscript` inside the `head` to provide alternatives for JavaScript functions, as that would involve writing content inside the head element." - http://reference.sitepoint.com/html/noscript
Yi Jiang
@Jiang Sorry, but I tested now and works. At least in firefox. It isn't W3C compilant, but works. :)
Topera
@Topera - Well, I'd change my words to "You *shouldn't* do that" then ;)
Yi Jiang