I'm injecting a script like so:
var script = $('<script>', {
type: 'text/javascript',
async: true,
src: 'https://script.js'
});
$('script:first').before(script);
This generates markup like:
<script type="text/javascript" async="async" src="https://script.js"></script>
I would prefer the following syntax:
<script type="text/javascript" async src="https://script.js"></script>
Is this supported when passing options to a jQuery DOM element creator? Or, should I just use plain JavaScript to achieve this?
You can set the attribute using plain JS. Doing it through jQuery will auto-populate the value.
var script = $('<script>', {
type: 'text/javascript',
src: 'https://script.js'
});
script[0].setAttribute("async", "");
$('script:first').before(script);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With