I would like to include js file from different location that it's now. Currently in master page code it looks as below:

<SharePoint:ScriptLink ID="ScriptLink1" language="javascript" name="Company/Scripts/myJsfile.js" Localizable="false" runat="server"/> 

and it works fine.

But what I'd like to do is to change current location of this javascript file. Target location of this .js file is a shared location (" \v-share\share\javascript ").

Is there any way to include this .js file from shared location in sharepoint master page ? Or is there any possibility to include .js file from location different than /_layouts/.. path (e.g. "C:\Users\Desktop\myJsfile.js")?

I would be very thankful for your help, because this case is very importan for me.

  • Conrado: you can't register javascript file with links other that http(s)://. Actually, you could wrap the actual location of the javascript with a custom handler that will server the javascript over http, but it does not seems to be a best practice to me. If you don't want javascript to be stored in SharePoint, create a web application somewhere that will server the JS files. A bit like CDNs do. – Steve B Jun 22 '12 at 14:53
  • And how I should define calling javascript file then? Like this: 1. <pre><SharePoint:ScriptLink ID="ScriptLink1" language="javascript" name=http(s)://Company/Scripts/myJsfile.js Localizable="false" runat="server"/><code> or 2. <script src=http(s)://Company/Scripts/myJsfile.js type="text/javascript"> </script> – Conrado Jun 25 '12 at 7:29

Why don't you Store your js files in Style Library of Site Collection's root web. In such way it can be easily accessed and versioned in case of updates. See below

<SharePoint:ScriptLink ID="ScriptLink1" Name="<% $SPUrl:~SiteCollection/Style Library/Core Styles/jquery.js%>" runat="server" />"

If you can deploy to _layouts (i.e. not restricted to sandbox) and you anticipate needing these libraries across many site collections, I would deploy them to a folder within _layouts. Generally, it's good practice when deploying into _layouts to deploy to a subdirectory within _layouts, something like "_layouts/MYIVO"

<SharePoint:ScriptLink ID="ScriptLink1" Name="~SiteCollection/_layouts/MYIVO/Scripts/something.js" runat="server" />

  • Hey Falak, I'm using the link below to include my custom js file in a user control and it works fine, but NOT with an application page! Do we include js in an application page the same way or is it different? <SharePoint:ScriptLink ID="JsToAspx" runat="server" Name="(Path to the js file stored in _layouts)"></SharePoint:ScriptLink> Thanks in advance! – Eliya Amanoeel Apr 30 '13 at 8:32
  • 1
    Eliya, please see my updated response! – Falak Mahmood Apr 30 '13 at 9:12

You will want to keep it in SharePoint. Putting it on your local c drive will not resolve for other users. Typically people will load it into the specific SharePoint site if it is local, or into the Layouts directory if its globally accessed.

  • But the problem is that this .js file needs to be in shared location. SSIS package from other server updates data in the js file (the second server has also access to this shared location and it updates javascript every hour). – Conrado Jun 22 '12 at 14:26
  • Why is an SSIS package updating your js file every hour?!?!? You will have all kind of problems with caching and could have multiple versions of the js file executing all of the time. – Mike Oryszak Jun 22 '12 at 14:28
  • I mean the package updates some variables only. But my issue is how to include a javascript file from shared location ? – Conrado Jun 22 '12 at 14:32
  • I don't recommend this but can you move your shared location inside the layouts folder? – Vardhaman Deshpande Jun 23 '12 at 13:13
  • It would be the easiest solution, but unfortunatelly it's not possible. – Conrado Jun 25 '12 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.