Jump to content

Topic on Project:Support desk

How do I add a custom script at the opening of the <head> tag on all pages?

12
173.73.54.201 (talkcontribs)

Need to add a short JS script that loads almost instantly on page-load. It needs to be as soon as the <head> tag begins.

Nicole Sharp (talkcontribs)

Not sure if this would work, but try using the Google Analytics Extension maybe. It allows you to load custom JavaScript for tracking codes, but it might work for other things too. Nicole Sharp (talk) 16:06, 20 March 2018 (UTC)

173.73.54.201 (talkcontribs)

Thank you Nicole. I found this extension: Extension:HeadScript. Does anyone have experience with it? It looks like it *might* do what I need.

Nicole Sharp (talkcontribs)
Nicole Sharp (talkcontribs)

Just tried out Extension:HeadScript. It works for Google Analytics with a Global Site Tag. You can use it for multiple scripts. FYI, the new Google Analytics Global Site Tag is not compatible with the Google Analytics Extension. Nicole Sharp (talk) 14:58, 21 March 2018 (UTC)

Chuck.Kahn (talkcontribs)

Adding script via the Extension:HeadScript method causes my wiki to http 500 error and adding via MediaWiki:common.js I don't see any of the google adsense or analytics scripts in the header when I go to view source (of other pages on my wiki). Is my internet host blocking this?

AhmadF.Cheema (talkcontribs)

The "HTTP ERROR 500" could be due to some incompatibility in the extension version, or even due to a syntax error in your appended code.

Can you share the code (with private data removed) which you add to your LocalSettings.php file?

Chuck.Kahn (talkcontribs)
AhmadF.Cheema (talkcontribs)

I was referring more to the code surrounding the JavaScript code, to see whether there was any syntax mistake in the php code, like the code line used to enable the extension and $wgHeadScriptCode line.

Chuck.Kahn (talkcontribs)

Of that pastebin, it seems the "gtag" lines 6 and 8 are the problem. If I leave them out, the page loads. If I leave them in, then "HTTP ERROR 500".

Chuck.Kahn (talkcontribs)

Changing the ' marks to \' in lines 6 and 8 got rid of the HTTP ERROR 500.

192.166.87.116 (talkcontribs)

I am interested in this question.

Ist there a way to add a new <script> tag without using an extension?


Thanks!

Reply to "How do I add a custom script at the opening of the <head> tag on all pages?"