Add JavaScript code on all the Pages of the Forum

Started by NeonFlash, September 19, 2017, 03:36:17 PM

Previous topic - Next topic

NeonFlash

Hi,

I would like to insert a few lines of JavaScript code so that it is included in the source code of all the pages of the Forum. Can this be done through the Admin panel of the forum?

Unfortunately, I don't have access to my Cpanel right now and I have requested the shared hosting provider to reset my credentials.

Thanks.

Arantor

Possibly but it depends what code and where it should be because there are multiple places where this possibly could go without breaking SMF's JavaScript... more details please.

NeonFlash

So, this code doesn't display anything on the web page. It is something like shown below. It just references an external JavaScript which has a class defined in it. It instantiates an object from it and then invokes a method.

<script src="http://example.com/package.js"></script>
<script>
var myobj = new Package();
var count = Package.count();
...
</script>

Arantor

What code is it? Is it render blocking? Should it be instantiated as async? I'm getting the impression it should be in the footer to be after the main page execution, and not block rendering or eventing in the main thread.

(You don't need to tell me that it references an external JS file and instantiates a class, the code snippet tells me that. I also helped write some of the SMF code...)

NeonFlash

@Arantor: I finally obtained access to my Cpanel. Could you tell me how I can update the files through Cpanel to include this code?

I think by render blocking and async, you mean whether it would prevent the web page from loading until the operation completes? If so, then that's not the case here.

Kindly let me know which files I would need to update. I think, we would need to update the Forum template files so that it is reflected on all the pages of the Forum. Please help with the details as I am doing it for the first time. Thanks in advance.

Arantor

It's *completely* the case. The minute you hit a <script> tag that has inline code, it blocks rendering until the code is executed. So it *matters*.

And this is why I want to know what that code should be doing before I point out where you need to change to avoid damaging your forum.

Advertisement: