开发者

redirect to a page if browser not supports javascript in asp.net MVC

How to check whether the browser supports javascript?

I need to redirect the users to a Notification Page, if the user's browser don't support javascript. How can i do this in my asp.net mvc(C#) application?

Whats the best way to handle this in asp.net mvc?

The html i tested:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Stack Overflow</title>
    <link rel="stylesheet" href="http://sstatic.net/so/all.css?v=6230">
</head>
<body>
    <noscript>
        <div id="noscript-warning">Stack Overflow works best with JavaScript enabled<img src="http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno"></div>
    </noscript&g开发者_如何学编程t;        
</body>
</html>


Wow at all the -1. Erm, well... it may not be valid, but the meta-in-noscript hack (as everyone's already posted and been voted down for) really is the only way to do what you want. But:

I need to redirect the users to a Notification Page, if the user's browser don't support javascript

I think what you want isn't what you need. No-JS redirection is horrible for usability/accessibility. Please don't do that.

Consider an approach like SO's instead: keep the user on the same page, but just include an on-page notification that scripting is unavailable:

<noscript>
    <div id="noscript-warning">Stack Overflow works best with JavaScript enabled</div>
</noscript>

You can use CSS to make this big and red and appear on top of the whole of the rest of the page if you have to.

You can be more specific too. Often you need not only JavaScript, but support for certain scripting features that aren't available everywhere. For example IEMobile (pre-8) does have JavaScript, but has stunted DOM support which stops you running most modern scripts. Or you might be relying on HTML5-related interfaces that aren't everywhere yet. In that case you can sniff client-side and set the notification's visibility manually:

<head>
    <style type="text/javascript">
        #scriptwarning {
            position: absolute; z-index: 9;
            top: 25%; left: 25%; width:50%; height: 50%;
            color: red; background: white;
            border: dashed red 1px;
        }
        body.jsok #scriptwarning { display: none; }
    </style>
</head><body>
    <script type="text/javascript">
        // Sniff capabilities in whatever way is necessary
        //
        if ('featureWeWantToUse' in window) {
            document.body.className= 'jsok';
        }
    </script>
    <div id="scriptwarning">
        JavaScript is disabled, or too rubbish on your browser to do what we want.
        See the <a href="browsersupport.html">supported browsers</a> page for more information.
    </div>
</body>


There's no way to detect it server-side, I don't know of any browser that communicates its javascript support status to the server in the headers it sends. You should be able to use a meta refresh inside a noscript tag though:

<noscript>
    <meta http-equiv="refresh" content="0; URL=no_javascript.asp">
</noscript>

As an alternative, I would recommend doing what SO does - display a big red banner at the top of the page saying that JavaScript is required or makes the page work better.

<noscript>
    <div id="noscript">Enable JavaScript to get the most out of this site!</div>
<noscript>


Alternatively you can use the noscript tag to hide the page content and display a warning that the page requires javascript. You can do something like this :

<noscript>
  <style> 
     #id_of_content_to_hide { display:none; }
  </style>
  <p>
     This page requires javascript.
  </p>
</noscript>


It's very easy to turn this on its head.

Start on the page that is there for people who don't have JavaScript, then use JavaScript to redirect to the version that requires JavaScript.

<script type="text/javascript">
    document.location = "JSVersion.html";
</script>


Put a meta refresh in a noscript tag.


I'm not really sure how you want to redirect, but you can use the <meta> tag to redirect to another page: <meta http-equiv="refresh" content="2;URL=page.aspx" />

The URL can be created by the <%=Url.Action(...)%>


Just going to throw this idea out there...and I am prepared for lots of down votes as this is a way to find out if my idea is a very bad idea or not :)...

  1. Create a meta refresh to "no-java.html"
  2. Have a small script that deletes the meta tag (document.querySelector("[name='this.meta.tag']").remove() sort of thing...)

If no JS allowed, the meta is fired otherwise it is deleted.


A very hacky approach would be to:

  1. Set a cookie using JavaScript

  2. Use a meta refresh to redirect (this is the worst possible way to redirect, but the only one that will occur after some JS runs if JS is available and still work if JS is not available)

  3. Check for the presence of the cookie on the page being redirected to and issue a 302 to either a JS present or a JS not-present page.

This would fail if cookies or JavaScript were disabled or unsupported though.

If you step back and try to solve the problem of "Some users don't have JS" properly (rather than going directly to "Try to detect users without JS") then use progressive enhancement and follow rule 2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜