Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing the script tag type and applying it

Is there any way to replace the script type with javascript only and execute it

this is an example script

<script id="myscript" src='myscript.js' type='text/template'></script>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i want to replace type with text/javascript i tried this code to replace it and it does replace the type but the script is not applied to the site.

<script>document.querySelector("#myscript").setAttribute("type","text/javascript");<script>

>Solution :

Make a new script instead, one with the proper type.

const existing = document.querySelector("#myscript");
const newScript = existing.cloneNode();
newScript.type = 'text/javascript';
existing.replaceWith(newScript);

Or just take the src of the existing one.

const existingSrc = document.querySelector("#myscript").src;
document.body.appendChild(document.createElement('script')).src = existingSrc;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading