2

I'm trying to run a simple script on a document library on a team site. I'm quite new to doing anything with sharepoint other than using OOTB features, so forgive me if this is a painfully simple question.

I've added the script link by entering:

< SharePoint:ScriptLink language="javascript" name="~sitecollection/SiteAssets/custom.js" runat="server" Localizable="false" />

under the head tag in 'seattle.master' in SPD. I added the code to my custom.js file which I uploaded to my site assets library on the root site.

I'm not achieving my expected result.

My question is: Am I making any obvious mistakes, or does the error most likely reside in my code?

3

The ScriptLink tag looks for file under '_layouts' folder. You could simply use HTML script tag to add reference to your custom file.

Scenario:

Root site collection: https://sp2013.sharepoint.com

Site Collection One: https://sp2013.sharepoint.com/sites/SiteCollectionOne

Site Collection Two: https://sp2013.sharepoint.com/sites/SiteCollectionTwo

Put your assets (images, scripts, css) folder inside the root site collection's Site Assets and give access to 'Everyone' for this folder (MyCompany)

enter image description here

https://sp2013.sharepoint.com/SiteAssets/MyCompany

Assuming you put your custom javascript file(s) under https://sp2013.sharepoint.com/SiteAssets/MyCompany/scripts

Now you could refer your assets from any master page from any site collection as:

<script src="/SiteAssets/MyCompany/scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="/SiteAssets/MyCompany/scripts/custom.js?rev=03_30_2014" type="text/javascript"></script>

In the above line, you may have noticed the rev tag after js file reference, it's so to handle CSS/JS caching issues without asking users to do Ctrl+f5. In case you're to update CSS/JS, you update revision id (rev tag) in the master page(s).

3
  • Thank you so much for your help, Aveenav. I'm really learning all this as I go, and it can be really hard to find documentation that applies to sharepoint online specifically. I never would have got anything working without people like you. THANKS!
    – Dave
    Mar 31 '14 at 14:52
  • 1
    As a side note, so long as you use the ~sitecollection token in the ScriptLink control it will parse the token into the site collection's server relative URL and will not try to load from the file system. Mar 31 '14 at 16:21
0

To see if your script is being loaded, check the Network portion of the Developer Tools in IE (or Firebug, etc.) If the file is being loaded then you've added the script link correctly.

If it's loaded correctly and you're still not seeing what you expected, check the console for errors, or use the debugger to troubleshoot.

If your code isn't being hit, you need to hook into an event or set the script to run on page load using either _spBodyOnLoadFunctionNames.push() or ExecuteOrDelayUntilScriptLoaded() depending on your scenario (the latter waiting to execute until after the script specified as the second parameter is loaded).

0

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.