最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create script tag with async attribute - Stack Overflow

programmeradmin3浏览0评论

I'm injecting a script like so:

var script = $('<script>', {
    type: 'text/javascript',
    async: true,
    src: ''
});

$('script:first').before(script);

This generates markup like:

<script type="text/javascript" async="async" src=""></script>

I would prefer the following syntax:

<script type="text/javascript" async src=""></script>

Is this supported when passing options to a jQuery DOM element creator? Or, should I just use plain JavaScript to achieve this?

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?

Share Improve this question edited Oct 6, 2014 at 19:22 Huangism 16.4k7 gold badges50 silver badges75 bronze badges asked Oct 6, 2014 at 19:14 Sean AndersonSean Anderson 29.3k33 gold badges131 silver badges242 bronze badges 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. – orhanhenrik 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. – Sean Anderson Commented Oct 6, 2014 at 19:18
  • Looks like this should work: stackoverflow.com/questions/13159180/… – orhanhenrik Commented Oct 6, 2014 at 19:19
  • 1 It doesn't generate markup at all. It adds a script element to the DOM. – Quentin Commented Oct 6, 2014 at 19:24
Add a comment  | 

2 Answers 2

Reset to default 20

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);
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()    
发布评论

评论列表(0)

  1. 暂无评论