How to add a script at the beginning of page header?

I need to add a script for an existing cookie consent solution (provided by consentmanager.net).

It has to be placed as the first overall element in the page header.
I’ve tried the different options in the admin tool, in HEAD, HEADER, AFTER HEAD, BODY.
But none of them did work.

Placed in HEAD the script is being published in the middle of the header items, below a bunch of discourse scripts and other stuff. It blocks other custom content (here: Font Awesome icons), no matter which cookie setting I choose, but that’s it.

Placed in HEADER there’s no functionality at all.

Placed n AFTER HEAD I can’t even find the script in the rendered source code.

Placed in BODY (there’s a different script for this location) it’s being published much too late, near the end of the page. But it should go first in the body, if it’s not possible to add the script in the header.

So no success at all implementing my little script.
Can anyone tell me if there is a chance to add a script at the beginning of the head section?

Thanks a lot it advance!

2 Likes

Hello @SchweizerSchoggi I just tested adding a cookie consent banner from consentmanager.net in my site, and it worked when using the script from the Semi-automatic blocking tab.

If you use the other code, it blocks the entire site. You may be able to solve it with some custom JS to insert the script, but it’s easier to just use the other code snippet.

2 Likes

Thank you @cocococosti
Yeah, the semi-automatic blocking script could be an option - but where do you place it in the Discourse Admin tolol?

I have tried inserting it to the section
Customize => Theme => Edit CSS/HTML => Body

but it’s being inserted too late then. It should be integrated directly after the opeing body tag, but in fact it has been inserted just before the closing body tag.

You have to do it in the Head: Customize => Theme => Edit CSS/HTML => Head

Or create a theme component with the script (the process to add it to the head is the same) and add it to your theme: Beginner's guide to using Discourse Themes

Does not work for me in the HEAD section. Neither section seems to work, the script is being integrated too late either in the HEAD or in the BODY :frowning:

@SchweizerSchoggi Since you are a hosted customer, I can log into your site and take a look if you wish. Could you send me a PM with the script code please?

1 Like

After adding the cookie consent script to the theme, I checked the browser’s console and saw multiple scripts from the Cookie consent manager were being blocked due to Content Security Policy. I added those to the content security policy script src site setting and the cookie consent banner is displayed correctly now.

4 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.