This article specifically refers to the ScriptLink available in MOSS 07, as 2010 has other features such as script on demand which I will talk about another time.

If you are registering a script in your SharePoint Master Page, you basically have two options available to you. You can load it in this nice and  fancy SharePoint way or just use the good old

We’ll i guess it really could depend on when you are looking to load your js file. With ScriptLink you have the ability to specify when you want the script to load asynchronously or after the after the page is ready. The ScriptLink will always look in /_LAYOUTS/1033 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 the instant 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>

So who cares when the file loads? well you might.. For instance if you’re using some JavaScript that relies on specific page elements to be there to operate on, then you would definitely want your script loaded after. This can be achieved easier just by specifying defer=true. Of course there are ways around this which you can code into your JavaScript or jQuery files to handle this as well.

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.

So overall I don’t believe that there is much benefit from using the SharePoint:ScriptLink in Moss 2007, I personally use the to load all my custom js files. The jury is still out for 2010. I have yet to spend enough time experimenting with the newly added properties.

Comments
  • Timo Pitkäranta
    Posted at 8:27 am October 30, 2012
    Timo Pitkäranta
    Reply
    Author

    Hi,

    “The ScriptLink will always look in /_LAYOUTS/1033 for the scripts.”

    This is true only when the SharePoint language is set to English, for multilingual site you would have to copy the same script to each LCID folder, e.g. Finnish would be /_LAYOUTS/1035, Swedish /_LAYOUTS/1053 etc. So this is yet another reason to use regular javascript file reference instead of ScriptLink-tag

    • Thomas Daly
      Posted at 4:42 pm November 7, 2012
      Thomas Daly
      Reply
      Author

      thx for the feedback!

      The ScriptLink has a property -> Localizable where you can set it true or false. I believe that false looks for the scripts in the /_layouts folder and true would look at the localization folder. Looks like that was available for 2007 and is still there for 2010.

      I’ve never had to implement anything as of yet using other languages so I’m not certain on the technicalities of making that work.

  • Rob Dixon
    Posted at 9:25 pm June 5, 2012
    Rob Dixon
    Reply
    Author

    I have tried to use this approach in my SP2010 environment, and it is not working for me.

    I placed the following code in the master page:

    I have made changes to some of the functions

    • Thomas Daly
      Posted at 1:52 pm June 6, 2012
      Thomas Daly
      Reply
      Author

      Rob – i wrote this mainly for 2007, I’ve been meaning to update it for 2010 but haven’t had time yet. Looks like your comments might have been stripped as well. Send me your issues – thomas.m.daly@hotmail.com, I’ll see if i can help.

  • Thomas Daly
    Posted at 9:05 pm April 28, 2011
    Thomas Daly
    Reply
    Author

    Jan,

    Interesting findings.. I have not run into what you’ve mentioned here. The ScriptLink defer is said to defer for core.js.

    Also depends on the MasterPage and the order of the when the scripts are registered. Looking at a collection of masterpages here I see some are registering core.js and some if have that I believe are heather solomon based that are registering init.js.

    I’d be happy to take a look at what you have and maybe come up with a more concrete answer, feel free to email me a snippet or screenshot of masterpage and possibly the custom js file.
    -td

  • Jan Vanderstappen
    Posted at 12:32 pm April 27, 2011
    Jan Vanderstappen
    Reply
    Author

    I noticed that there is a difference between the two methods.
    I have a custom javascript file that overwrites some default function’s that are located in the OOTB init.js file.
    Because I needed javascript to use my custom function I needed to load my file after the init file.

    When I used the scriptlink function (see below) it did not work. My custom file seemed to be loaded before the init.js file causing that the functions of the init file were used.

    when I used the old method using the script tags, it did work.

    I absolutely have no idea why it does work with the script tags, so if you can figure an explanation, it is greatly appreciated 🙂

    Greetings,

    Jan

  • Leave a Reply to Thomas Daly
    Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.