Body Loading of JavaScript

Shannon Dunn asked on February 17, 2014 13:55

I have a question regarding loading of JavaScript. I have some JavaScript that I load on all pages of my site that is required to be loaded before the closing body tag. So I have in the past loaded it into the Master Page tab of my root page template. On a few templates I have some different libraries that are also required to be loaded before the closing body tag and they all must be loaded before my main.js is loaded which uses them. So my question is, without loading every JavaScript library in the root of my site for every page, how can I properly load these libraries and in the right sequence? The JavaScript web part has no option for putting the code in the bottom of the page.

Recent Answers


Shmar Hill answered on February 17, 2014 14:34

Hi,

You can do the following:

1) On your Master page add a placeholder webpart just above the JavaScript you are currently loading.

2) on the inner page template, add javascript webpart to the newly added placehoder area.

That should od the trick.

good luck!

0 votesVote for this answer Mark as a Correct answer

Milan Kačurák answered on February 17, 2014 16:00 (last edited on December 10, 2019 02:30)

  1. Put 2 JavaScript web parts into your Master page.
  2. Set both "Linked file page location" to "Startup script".
  3. In the first of those JavaScript web parts set "Linked file" to your subpage script file.
  4. In the second one set "Linked file" to your main.js script file.
  5. And then in the first of them set "Visibility" property to macro which ensures showing/hiding webpart holding your subpage script file. Eg. {% currentdocument.documentname == "Home" |(identity)GlobalAdministrator%} in the "Visibility" property shows your web part only on document called "Home".
4 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.