6

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?

4
  • 2
    Why do you want the other syntax? The easiest workaround is probably making the whole thing a string instead of an object that jquery converts. Commented Oct 6, 2014 at 19:17
  • Partially just curious, but also all other scripts on my page are loaded with requireJS which styles the script using the alternative syntax. It was hard for me to quickly skim all my scripts when one was different. Commented Oct 6, 2014 at 19:18
  • Looks like this should work: stackoverflow.com/questions/13159180/… Commented Oct 6, 2014 at 19:19
  • 1
    It doesn't generate markup at all. It adds a script element to the DOM. Commented Oct 6, 2014 at 19:24

2 Answers 2

21

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);
Sign up to request clarification or add additional context in comments.

Comments

0
from turtle import *
import colorsys

speed(0)
bgcolor("black")
h=0

for i in range(16):
    for j in range(18):
        c=colorsys.hsv_to_rgb(h,0.9,1)
        color(c)
        h+=0.005
        rt(90)
        circle(150-j*6,90)
        lt(90)
        circle(150-j*6,90)
        rt(180)
    circle(40,24)
    
done()    

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.