external js file doesn't work

Discussion in 'JavaScript' started by Nerve, Dec 10, 2010.

  1. #1
    I have a script that replaces certain text with a hyperlink and it works perfect if I put the javascript code at the footer, but if I put the javascrpt in an external file it won't work.

    For example here is the body section of the code that works (when the JS is in the footer)
    
    <body>
    one of these words will be replaced with a link
    
    <script type="text/javascript">
    (function($) {
      var thePage = $("body");
      thePage.html(thePage.html().replace(/replace/ig, '<a href="http://google.com">word</a>')); 
    })(jQuery)
    </script>
    </body>
    
    Code (markup):
    That works, however since I am going to be using this on many different pages I would rather link to the javascript file instead of having it on the page.

    I have tried this and it doesn't work (I just copied and pasted the same JS code onto the javascript.js file):
    
    <body>
    one of these words will be replaced with a link
    
    <script type='text/javascript' src='javascript.js'></script>
    
    </body>
    
    Code (markup):
    And then the javascript.js file (the file I'm linking to is the exact same:
    
    <script type="text/javascript">
    (function($) {
      var thePage = $("body");
      thePage.html(thePage.html().replace(/replace/ig, '<a href="http://google.com">word</a>')); 
    })(jQuery)
    </script>
    
    Code (markup):
    I'm using I'd have to modify the script to open the page where I am going to replace the text? Any help would be awesome!
     
    Nerve, Dec 10, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    remove <script></script from your js file (just the tags , not whats inside them...)
     
    tvoodoo, Dec 11, 2010 IP