Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I tried to reference the js files in my SharePoint pages but no way. I tried the follwing links:

<script type="text/javascript" language="javascript" src="/_layouts/customscriptfolder/scriptfile.js"/>
<script type="text/javascript" language="javascript" src="/SiteAssets/scriptfile.js" />
<script type="text/javascript" language="javascript" src="/Style Library/js/scriptfile.js" />

//and

<SharePoint:ScriptLink id="ScriptExternalJs" runat="server"  Name="/Style Library/js/TestScript.js" Localizable="false" ></SharePoint:ScriptLink>
<SharePoint:ScriptLink runat="server" Name="/scripts/TestScript.js"></SharePoint:ScriptLink>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Use ScriptLink with URL tokens to properly reference JavaScript files.

Examples

To specify the URL of the parent site collection of the current website, use ~sitecollection token:

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

To specify the URL of the current website, use ~site token:

<SharePoint:ScriptLink language="javascript" name="~site/SiteAssets/Script.js" runat="server"  />

To specify the URL of the Layouts virtual folder for the current website, use ~layouts token:

<SharePoint:ScriptLink language="javascript" name="~layouts/Contoso/Script.js" runat="server"  />

References

URLs and tokens in SharePoint 2013

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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