Any idea why the piece of code below does not add the script element to the DOM?
var code = "<script></script>";
$("#someElement").append(code);
Any idea why the piece of code below does not add the script element to the DOM?
var code = "<script></script>";
$("#someElement").append(code);
Try this may be helpful:
Adding the sourceURL in the script file helped as mentioned in this page: https://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/
Can try like this
The only reason you can't do
"<script></script>"
is because the string isn't allowed inside javascript because the DOM layer can't parse what's js and what's HTML.You don't need jQuery to create a Script DOM Element. It can be done with vanilla ES6 like so:
It doesn't need to be wrapped in a
Promise
, but doing so allows you to resolve the promise when the script loads, helping prevent race conditions for long-running scripts.I've seen issues where some browsers don't respect some changes when you do them directly (by which I mean creating the HTML from text like you're trying with the script tag), but when you do them with built-in commands things go better. Try this:
From: JSON for jQuery
Just create an element by parsing it with jQuery.
Working example: https://plnkr.co/edit/V2FE28Q2eBrJoJ6PUEBz