Adding inheritable javascript code to head

Brandon White asked on August 30, 2018 20:46

We need to add a tracking script to a specific section of our site including all of its subpages into the head of the page. Normally we'd just insert the html head widget onto a page but this time we need it to go on all subpages and there are several hundred of them. I was thinking of somehow doing this in the Form tab with a custom form control or webpart tied to it. Has anyone done this before or have any ideas?

Recent Answers


Brenden Kehren answered on August 30, 2018 20:51

Do your sub pages inherit the parent template? If so you can add a HEAD HTML webpart to that parent page and include the js that way.

0 votesVote for this answer Mark as a Correct answer

Brandon White answered on August 30, 2018 21:04

No they don't...the subpages could be any number of different templates.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 30, 2018 21:07 (last edited on December 10, 2019 02:31)

Then it may be easier to have the HEAD HTML webpart or javascript webpart on the master page and set visibility based on the path.

{%CurrentDocument.NodeAliasPath.Contains("/your-parent/path-to-children")|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Brandon White answered on August 30, 2018 21:30

This solution would have to be reusable - so next time we can add a different tracking script to another section of the site. I was thinking of creating a text field on the page type(s) and then tying that to a webpart or form control? We do this already I just have to figure out how it was done.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 30, 2018 21:37

If each page below a section will have the same tracking script and none of the pages below inhert from that parent page then even if you have a new field to the page type, you still have to modify each page template to accomodate that new field. So although good, it still requires touching all the page templates.

0 votesVote for this answer Mark as a Correct answer

Brandon White answered on August 30, 2018 21:44

It looks like that is how we do it for another use case....custom web part and then that web part is added to all templates and then the form tab has a field where we set options that inherit. Not sure if this is ideal but that's how it was done.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on August 30, 2018 21:52

Having to modify every template, and then rely on developers to add a web part to every page going forward is just asking for something to be missed if you ask me.

You could possibly combine that with Brenden's master page suggestion, just add the custom web part to the master template and pull a value from the current page.

0 votesVote for this answer Mark as a Correct answer

Brandon White answered on August 30, 2018 21:55

Yeah that sounds much better, than adding to all pages. Not sure why we didn't do that in this other one. I'm going to try it and see if I can make it work in the master page.

0 votesVote for this answer Mark as a Correct answer

Brandon White answered on September 5, 2018 15:12

I was able to do this with a custom web part inserted on the master page and then adding fields to the page types so the code can be entered on the form tab and inherited to child pages. Works great, no more manual entry for multiple pages that need a tracking script.

0 votesVote for this answer Mark as a Correct answer

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