Loading an external JS file on a page

Hi, I’m using the HTML Snippet widget to load an external JS file on a page. It doesn’t look like it’s actually executing the JS in the file. I loaded the same script in theme/index.html and it loads fine, the problem is I don’t want the JS on all the pages in my project. I have tried putting the script in html and pure javascript too. Is there a better way to load JS on specific pages or will I have to deal with it on all pages? Thanks   Edit 1: Here is how I’m loading the file. JS is:  function initFreshChat() { window.fcWidget.init({ token: "5e5a****-****-****-****-******fc72ab", host: "https://wchat.freshchat.com" }); } function initialize(i, t) { var e; i.getElementById(t) ? initFreshChat() : ((e = i.createElement("script")).id = t, e.async = !0, e.src = "https://wchat.freshchat.com/js/widget.js", e.onload = initFreshChat, i.head.appendChild(e)) } function initiateCall() { initialize(document, "freshchat-js-sdk") } window.addEventListener ? window.addEventListener("load", initiateCall, !1) : window.attachEvent("load", initiateCall, !1);  
2 answers

Your initial attempt is the usual way. It looks like this:

Not sure where you went wrong. Add some more information and screenshots of your HTML-snippet with the JS.


Tim’s solution is easiest.


It is possible to create a setup where you can do this sort of stuff dynamically

For this you have to add some js that listens to page change, checks the page in the db, if it has a script ,execute it

I also have a thing where I can load in libs referenced by these page scripts, these are configured and served up via the rest service/request handler, so you can build/store libs and reference these on your page scripts