1

I am using a web app for my ecommerce store where they only allow you to add javascript via their input box. You are suppose to add javascript without any script tags.

However, I am trying to integrate a 3rd party analytics tools using the following code:

<script type="text/javascript" src="xxxx" async></script>

How can I add this to the input box without using script tags though? Are there alternative methods?

Thks

3
  • @Taplar — The CMS only allows JS to be added by entering the JS source code directly into a text box on a form. Commented Sep 23, 2020 at 16:39
  • you probably just add the source for your javascript. So just input the url to that library using a CDN and it will probably work fine Commented Sep 23, 2020 at 16:40
  • @LelioFaieta — It a 3rd party analytics tool, so it probably depends on cookies being set by the Set-Cookie header on the response to the request for the URL for the script (hosted on the analytic services servers). Copying it to a CDN would break that. It would also break updates to the script. It also wouldn't solve the problem because using a CDN just changes the URL and the OP's problem is that they can't add a <script> tag to type the URL into the src attribute of! Commented Sep 23, 2020 at 16:45

1 Answer 1

6

Write JS to modify the DOM to insert the script element.

(function () {
  const script = document.createElement("script");
  script.src = "xxx";
  script.async = true;
  document.head.appendChild(script);
})();
Sign up to request clarification or add additional context in comments.

2 Comments

Would this work for older browsers (IE < 9 etc.) that don't support document.head? Seems like the following would be a better approach: document.getElementsByTagName("head")[0].appendChild(sc);
You could if you really wanted to support a beyond-end-of-life browser with a general market share as close to zero as makes no practical difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.