4

I added a custom script to my "Style Library" folder under a site collection. The site collection URL is http://servername/HR. So to reference the script inside my master page I wrote the following:

<script  type="text/javascript" src="/hr/Style%20Library/Custom JS/HideTreeViewFolder.js" ></script>

Where I am manually referencing the /hr/ inside the src, so can I make this more dynamic and get the site collection URL dynamically, so if I move the site collection to another URL the script will still get referenced?

1 Answer 1

7

I guess you could always use a site collection token in your URL.

~sitecollection

URLs and tokens in SharePoint 2013

If that don't work out for you, this blog post has a lot of great examples when working with masterpages and references. Use Site Url in SharePoint

Update

Try using a scriptlink instead. I'm unsure if encoding is needed thought. Try to have a blank space instead of %20 in /Style%20Library/.

<SharePoint:Scriptlink runat="server" Name="~sitecollection/Style%20Library/Custom JS/HideTreeViewFolder.js" Language="javascript" />
6
  • can you adivce more on this ,, i wrote the following src="~sitecollection/Style%20Library/Custom JS/HideTreeViewFolder.js" ,, but it did not work
    – John John
    Commented Jul 1, 2015 at 12:16
  • 1
    You have to use Sharepoint:Scriptlink as specified from user19952 Commented Jul 1, 2015 at 12:52
  • @displayName seems this is long approach to follow.. now i use to add my original script "<script type="text/javascript" src="/hr/Style%20Library/Custom JS/HideTreeViewFolder.js" ></script>" just before the end of </body> inside my master page.. now if i place the updated script on this location nothing will happen (the script will not fire)...so where exactly i need to place this script now ?
    – John John
    Commented Jul 1, 2015 at 13:04
  • as this script is intended to hide some components from my Tree view, so it is suppose to fire after all the scripts and DOM are rendered ,, so it can override the defualt behavior ..
    – John John
    Commented Jul 1, 2015 at 13:05
  • @johnG, try "~sitecollection/Style Library/Custom JS/HideTreeViewFolder.js" instead. Commented Jul 1, 2015 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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