Showing posts with label JS. Show all posts
Showing posts with label JS. Show all posts

Friday, March 10, 2017

When to use ScriptLink in SharePoint 2013

Hi Folks,

Some times we will run into situations where our custom js file should load only after the page is ready.

In this kind of scenario's, best thing to use is ScriptLink.

ScriptLink have the ability to specify, when we want the script to load asynchronously after the page is ready. By Default, the ScriptLink will always look for the scripts present in /_LAYOUTS/1033 folder.

<SharePoint:ScriptLink language="javascript" name="MyCustomJS.js" Defer="true" runat="server"></SharePoint:ScriptLink>

MyCustom.js should be present in /_LAYOUTS/1033 folder, if not we will get a SharePoint Error screen.

If you set the "Localizable" property of the ScriptLink to false, you can also reference files outside of /_LAYOUTS/1033/ and just put your path to the "Name" property

<SharePoint:ScriptLink Language="javascript" Localizable="false" Name="/_layouts/JS/MyCustomJS.js" runat="server"></SharePoint:ScriptLink>

With the above approaches, we can load the page first and then load the script file.

Note: Using Defer="False" is basically the same as loading the script with the usual method as below.
<script type="text/javascript" src="/_layouts/1033/MyCustomJS.js"></script>


Hope this helps...

Popular Posts