3

Due to performance problems, we decided to reference javascript files at the end of the master page file.

However, if we reference javascript files at the end of master page using ScriptLink Tag, the javascript files are always bound at the start of the page.

Is it possible to bind the javascript files at the end of page using ScriptLink Tag?

  • 1
    Why you want use ScriptLink? Are you JS custom files? – Salvatore Di Fazio Mar 27 '14 at 13:53
7

WHAT I PRESUME

1) you are working with custom js code

2) you are working with a custom page

SOLUTION

1) don't use ScriptLink :

If you register a .js file by adding a ScriptLink control to an .aspx page, the control registers all .js dependencies. To import the various "SP" namespaces, such as those listed at ECMAScript Object Model Reference, you must register the control as an on-demand script by specifying OnDemand="true". When you register the script, use the SP.SOD.execute or SP.SOD.executeFunc method to execute the script.

because, how you wrote, that link your script at the head of the page

2) In SharePoint 2010/2013, the correct way to execute function after script is loaded is to use something like this:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

3) Also you can register your custom js:

    SP.SOD.executeOrDelayUntilScriptLoaded(myfunction, 'sp.js');
    function myfunction() {
      // my custom code
      //
      SP.SOD.executeOrDelayUntilScriptLoaded(myfunction2, 'custom.js');
    }

    SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("sp.js");

Also with SP.SOD.registerSod you can register your own file: http://msdn.microsoft.com/en-us/library/ff411760(v=office.14).aspx

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.