开发者

Linking external JS to rails app

This should be a simple one but I cant get the syntax quite right.

I also cant find any examples of externally linked files, All search results I've found seem to solve their problems by storing a local copy of the file.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>

This is a simple external script tag in my .erb file but rails is not picking up anything that isnt using

<%= javascript_include_tag 'whatever.js' %>

How do I externally link up a JS to my rails app.

I cannot store the Jquery.js locally due to technical requirements (I don't understand it either)


 <%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' %>

Perhaps the problem wasnt me not referencing the file correctly. It seems to be generated in the html.erb file fine but in the firebug console, I'm getting Jquery not defined.

jQuery is not defined
[Break On This Error] remText=remText.replace(/\%n/g,charsRe...al:true,allowExceed:false};})(jQuery);
jquery...9702940 (line 17)
jQuery is not defined
[Break On This Error] })(jQuery); 
protot...0469242 (line 5653)

The generated HTML links. I have no idea why the standard src = tag wont find it.

     <link href="/stylesheets/globalStyleSheet.css?1312383253" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/jquery.inputlimiter.1.0.css?1312464674" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/style.css?1312469715" media="screen" rel="stylesheet" type="text/css" />
      <script src="/javascripts/prototype.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/effects.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/dragdrop.js?1310469242" type="text/javascript"></script>

    <script src="/javascripts/controls.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/rails.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/application.js?1310469242" type="text/javascript"></script>


    <link href="/stylesheets/globalStyleSheet.css?1312383253" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/style.css?1312469715" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/jquery.inputlimiter.1.0.css?1312464674" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/globalStyleSheet.css?1312383253" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/jquery.inputlimiter.1.0.css?1312464674" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/style.css?1312469715" media="screen" rel="stylesheet" type="text/css" />

    <script src="/javascripts/jquery.inputlimiter.1.2.2.min.js?1299702940" type="text/javascript"></script>
    <script src="/javascripts/globalviewFunctions.js?1312381897" type="text/javascript"></script>

    <script src="/javascripts/editInPlace.js?1312468960" type="text/javascript"></script>
    <script src="/javascripts/errorDisplay.js?1312381897" type="text/javascript"></script>
    <script src="/javascripts/journeyViewFunctions.js?1312541017" type="text/javascript"></script>
    <script src="/javascripts/pageviewFunctions.js?1312449379" type="text/javascript"></script>
    <script src="/javascripts/shaneShowHide.js?1312387899" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>

    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
    <script src="/javascripts/prototype.js?1310469242" type="tex开发者_高级运维t/javascript"></script>
<script src="/javascripts/effects.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/controls.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/rails.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/application.js?1310469242" type="text/javascript"></script>
    <script src="/javascripts/prototype.js?1310469242" type="text/javascript"></script>

<script src="/javascripts/effects.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/controls.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/rails.js?1310469242" type="text/javascript"></script>
<script src="/javascripts/Legacy.js?1312381897" type="text/javascript"></script>
<script src="/javascripts/editInPlace.js?1312468960" type="text/javascript"></script>
<script src="/javascripts/errorDisplay.js?1312381897" type="text/javascript"></script>
<script src="/javascripts/globalviewFunctions.js?1312381897" type="text/javascript"></script>
<script src="/javascripts/journeyViewFunctions.js?1312541017" type="text/javascript"></script>

<script src="/javascripts/jquery.inputlimiter.1.2.2.min.js?1299702940" type="text/javascript"></script>
<script src="/javascripts/jquery.min.js?1312544046" type="text/javascript"></script>
<script src="/javascripts/pageviewFunctions.js?1312449379" type="text/javascript"></script>
<script src="/javascripts/shaneShowHide.js?1312387899" type="text/javascript"></script>
<script src="/javascripts/application.js?1310469242" type="text/javascript"></script>


In one of my applications I also use the javascript_include_tag to include javascripts. Only difference is that I feed it an array (which gets dynamically filled). It works somethin like this:

def add_javascript(url)
  @javascript_urls ||= []
  @javascript_urls << url unless @javascript_urls.include?(url)
end

def create_javascript_urls()
  javascript_include_tag(@javascript_urls) + "\n" unless @javascript_urls.blank?
end

To add the jQuery javascript I use:

add_javascript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js')

It all seems similar to me compared to your question examples, but maybe it helps you in finding the answer...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜