在每个页面上显示“升级您的浏览器”模式

如果用户从旧浏览器访问我网站上的任何页面,我想向用户展示自定义的“升级浏览器”模式对话框。

我知道如何检测浏览器但是 – 我无法弄清楚将代码放在何处以显示模式以及如何执行此操作。 ApplicationController before_filter可以渲染一个调用.modal(’show’)的js吗? 还有别的办法吗? 纯粹的js?

使用Javascript可以最恰当地解决这个问题。

  • 使用Modernizr检测您需要的浏览器function
  • 如果Modernizr测试失败,则显示某种类型的弹出窗口

将modernizr.js添加到您的项目并将此javascript插入您的application.js:

Modernizr.load([ { test: Modernizr.cssgradients, nope: function () { alert('Sorry, your browser does not support a required feature of this site. Please upgrade or use another browser.'); } } ]); 

将“Modernizr.cssgradients”更改为您需要的任何function。

如果你不想把它作为一个“警告”,你可以使用像noty.js这样的东西 ,用上面的alert方法替换:

 noty({text: 'Sorry, your browser does not support a required feature of this site. Please upgrade or use another browser.'}); 

将div / modal放在布局文件中(app / views / layouts / application.html.erb,最有可能),并根据您的需要将其包装在rails条件中,如下所示:

  .... <% if (old_browsers.include?(current_browser) %> 
....
<% end %>