68968

Create script tag with async attribute

Question:

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?

Answer1:

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);

Recommend

  • position: fixed with margin: auto in IE9/10
  • Colorize knockoutjs comment bindings in code editor
  • C++ Boost ptr_map serialization error
  • Variant from android-autofittextview library : scaling makes strange behaviour
  • Usage of local dart library
  • Using multiple input pipelines in TensorFlow
  • HighCharts - Show tooltip on column where value is 0 or null
  • triggering user space with kernel
  • in batch how do i use taskkill properly
  • Local Development, Apache vs Developer - file permissions
  • is there a way to update filter with async data
  • Authentication failed with Azure Active Directory in Windows Phone
  • In loopback documentation what does variable 'cb' stands for?
  • ThreadStatic in asynchronous ASP.NET Web API
  • How to view images from protected folder with php?
  • Loading .coffee files via a view in Rails
  • Display images in Django
  • Uncaught Error: Could not find module `ember-load-initializers`
  • req.body is undefined - nodejs
  • RectangularRangeIndicator format like triangular using dojo
  • Resize panoramic image to fixed size
  • Cross-Platform Protobuf Serialization
  • Modifying destination and filename of gulp-svg-sprite
  • Importing jscolor library in angular 2
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Traverse Array and Display in markup
  • How to delete a row from a dynamic generate table using jquery?
  • Revoking OAuth Access Token Results in 404 Not Found
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • sending mail using smtp is too slow
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal