最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - Check if Javascript is available and redirect to another page - Stack Overflow

programmeradmin2浏览0评论

In my rails site, i require javascript on all the pages. If it is not enabled, every page will fail.

What I want to do is, if there is no javascript enabled, I want to redirect them to another static page - asking users to enable the javascript first. I want this to happen for all the pages. Is it possible ?

I have tried to put code in my application controller and checked but some how things are not getting into place.. Is there any standard solution to this in rails .. may be a helper or something ... ?

In my rails site, i require javascript on all the pages. If it is not enabled, every page will fail.

What I want to do is, if there is no javascript enabled, I want to redirect them to another static page - asking users to enable the javascript first. I want this to happen for all the pages. Is it possible ?

I have tried to put code in my application controller and checked but some how things are not getting into place.. Is there any standard solution to this in rails .. may be a helper or something ... ?

Share Improve this question asked Sep 1, 2010 at 9:07 AnandAnand 10.4k26 gold badges91 silver badges138 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

Just use the <noscript> element to show a link: <noscript>Your browser does not support scripting. Please use the <a href="#">lite version</a></noscript>. It's likely you'll have very few users without scripting, so it's not worth spending much time on these features.

As a NoScript user, I find these redirections to be extremely annoying. If I decide that, yes, I want to allow this site to use JavaScript, I simply want to click the “allow JavaScript” thingie in my browser, which will reload the current page. With redirections, I need another step.

What I do instead is to place some code like this at the top of the page:

<div class='warning' id='js-warning'>
  Sorry, this page really requires JavaScript to be useful.
  … maybe a little more text on why …
</div>
<script language='JavaScript'>$('js-warning').hide()</script>

One technique that I've used successfully is to have the first page that's accessed—typically a login form—write a session cookie using JavaScript when the page loads. Then have the controller that the form submits to check for the existence of that cookie. If it doesn't exist then you know that JavaScript is not available and you can take appropriate action.

Change url: ´NOSCRIPT.HTML´ to your taste.

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

Note: this is not valid HTML, though for this specific feature request, this is the only awnser. I could tell people on how to do it properly, but sometimes proper is not an option when a client is breathing down your neck and a pragmatic low cost solution is required ;)

That said, progressive enhancement is the proper thing to do and showing a message to enable javascript is definatly a cleaner way of informing your visitors to .. well enable JavaScript.

发布评论

评论列表(0)

  1. 暂无评论