I have seen an application page where the developer using scriptlink, and below that just , I was wondering when would each one be used?
2 Answers
It really could depend on when you are looking to load your js file. With ScriptLink tag you have the ability to specify when you want the script to load asynchronously or after the page is ready. The ScriptLink will always look in /_LAYOUTS/1033
folder for the scripts.
<ScriptLink language="javascript" name="MyJS.js" Defer="true" runat="server"/>
This line would load the page and then load the script file. If you set Defer="false"
then instantly the page loaded and hit that line the .js file would execute.
Using Defer="False"
is basically the same as loading the script with this method. But with this method below you can specify a location other that LAYOUTS/1033
if you wanted to
<script type='text/javascript' src="/_layouts/1033/myjs.js"></script>
Another difference with using the SharePoint:ScriptLink is that when the file gets loaded it must exist, otherwise you will get a SharePoint Error screen. This could help you ensure that SharePoint is seeing your js file
Besides referencing JavaScript files, your ASP.NET pages can have inline JavaScript code. Use the following pattern to make your script blocks
<SharePoint:ScriptBlock runat="server" >
// Your JavaScript code here.
</SharePoint:ScriptBlock>
-
Hi Aanchal, thanks for your response, but I didn't get the defer part as well as, when would the <script> be executed? Sep 7, 2014 at 14:00
-
1At the instant when the page is loaded and hit that line the .js file would execute.– AanchalSep 7, 2014 at 14:03
-
-
1033 is en-US locale, so it may be different, e.g. 1049 for ru-RU Mar 28, 2017 at 13:33
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/myFile.js" runat="server"></SharePoint:ScriptLink>