Rails 3有条件地包含Javascript文件 [英] Rails 3 Conditional Inclusion of Javascript files

查看:34
本文介绍了Rails 3有条件地包含Javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出在Rails 3的脑海中包含以下内容的最佳方法:

I'm trying to figure out the best way to include the following in my head in rails 3:

<!--[if IE]><script language="javascript" type="text/javascript" src="/javascripts/excanvas.min.js"></script><![endif]--> 

如您所见,我只希望javascript文件可以在IE中使用...所以也许我根本不应该这样做...我愿意接受建议.

As you can see, I just want that javascript file availabe to IE... so perhaps I shouldn't do it this way at all... I'm open to suggestions.

我正在使用它来指定要包含的默认javascript文件:

I'm using this to specify the default javascript files for inclusion:

config.action_view.javascript_expansions [:默认值] =%w(jquery rails jquery-ui jquery.flot)

config.action_view.javascript_expansions[:defaults] = %w(jquery rails jquery-ui jquery.flot)

无论如何,仅当用户使用IE时,我才能在默认值中指定此excanvas.js吗?

Is there anyway I can specify this excanvas.js in the defaults only if the user is using IE?

做到这一点的最佳方法是什么?

What's the best way to do that?

谢谢!

推荐答案

最好的方法是您的操作方式,即:

The best way is the way you're doing it, i.e.:

<%= javascript_include_tag :defaults %>
<!--[if IE]><%= javascript_include_tag "excanvas.min" %><![endif]-->

有条件的注释(而不是服务器端用户代理抓取)是向IE/特定版本的IE提供其他CSS/JS资产的最佳方法.某些代理服务器会修改User-agent标头,这意味着某些IE用户可能没有将 excanvas 文件插入:defaults 扩展中.这样插入意味着向所有IE用户提供文件,而不仅仅是用户代理与典型IE用户代理匹配的文件.

Conditional comments, rather than serverside useragent scraping, are the best way to go about serving additional CSS/JS assets to IE/specific versions of IE. Some proxy servers modify the User-agent header which means that some IE users might not have the excanvas file inserted into the :defaults expansion. Inserting it like this means all IE users will be served the file, not just those where the user-agent matches a typical IE useragent.

这篇关于Rails 3有条件地包含Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆