locked
Adding a JS file to a masterpage RRS feed

  • Question

  • I want to add a JS file to the masterpage, but for some reason I can't get it to load.  I've tried placing it in PlaceHolderAdditionalPageHead area, as well as using SharePoint:ScriptLink, but with SharePoint:ScriptLink I get the the error:

    Cannot make a cache safe URL for "1033/blah/blah.js", file not found. Please verify that the file exists under the layouts directory.

    Of course I don't want the file to live in the layouts directory as it's only for one website and not the whole farm.  So JS file just lives in Style Library/Scripts/ of that Site Collection.  Any thoughts in how I can bring it in?  Thanks!

    Wednesday, December 21, 2011 4:58 PM

Answers

  • Ok nevermind, I found it on Google.  Here's what I had to use to get it to read into Style Library folder.

     

    <SharePoint:Scriptlink language="javascript" name="~sitecollection/Style Library/Scripts/hi.js" runat="server" />

    • Marked as answer by zenicanin Wednesday, December 21, 2011 5:40 PM
    Wednesday, December 21, 2011 5:40 PM

All replies

  • Hi 

    Create a folder "JS" in your "Style Library" folder of your site

    // in case of root site

    <script type="text/ecmascript" src="/style library/js/Myjs.js" />
    

    // in case of other site

     

    <script type="text/ecmascript" src="/sites/[sitename]/style library/js/Myjs.js" />

    and you can cross check the accesibility by opening the url in browser 

     

    http://<servername>/style library/js/Myjs.js
    
    
    
    
    http://<servername>/sites/[sitename]/style library/js/Myjs.js" />

     

     


    Manoj Gangwar | Sharepoint 2010 MCPD | Sharepoint 2010 MCTS


    Wednesday, December 21, 2011 5:11 PM
  • It's not in the layouts, it's just in the style library of that site collection.  Basically, http://servername/Style%20Library/Scripts/hi.js


    I tried this code and it gives that error.

    <SharePoint:ScriptLink language="javascript" name="/Style%20Library/Scripts/hi.js" defer="true" runat="server"/>

    Wednesday, December 21, 2011 5:27 PM
  • Ok nevermind, I found it on Google.  Here's what I had to use to get it to read into Style Library folder.

     

    <SharePoint:Scriptlink language="javascript" name="~sitecollection/Style Library/Scripts/hi.js" runat="server" />

    • Marked as answer by zenicanin Wednesday, December 21, 2011 5:40 PM
    Wednesday, December 21, 2011 5:40 PM
  • ok :)
    Manoj Gangwar | Sharepoint 2010 MCPD | Sharepoint 2010 MCTS
    Wednesday, December 21, 2011 5:52 PM