I am" /> I am" /> I am"/>

clonenode script not execute in firefox

111 Views Asked by At

why clonenode script does not execute in firefox. it works fine in chrome?

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

I am cloning it by replacing the type='text/template' to 'text/javascript'

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

what can be the reason?

1

There are 1 best solutions below

2
T.J. Crowder On

It would appear that Firefox is cloning the flag saying that that script has been processed, but Chrome isn't. (I've been able to replicate your result.)

To do this reliably, create a new element instead:

const existing = document.querySelector("#myscript");
const newScript = document.createElement("script");
newScript.src = existing.src;
newScript.type = "text/javascript"; // Or just leave this line out, JavaScript is the default.
existing.replaceWith(newScript);