2

Load Dart Script if Dartium, Otherwise Load JavaScript in Browser

 2 years ago
source link: http://siongui.github.io/2015/02/14/load-dart-script-if-dartium-otherwise-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Load Dart Script if Dartium, Otherwise Load JavaScript in Browser

February 14, 2015

Assume that we write a dart script called app.dart, and the corresponding compiled JavaScript file is called app.js. We want the browser to load app.dart if the browser supports dart, otherwise load app.js for compatibility. How do we do?

Put the following code in your html before the end of body tag.

<script type='text/javascript'>
  var script = document.createElement('script');
  if (navigator.userAgent.indexOf('(Dart)') === -1) {
    // Browser doesn't support Dart
    script.setAttribute("type","text/javascript");
    script.setAttribute("src", "app.js");
  } else {
    script.setAttribute("type","application/dart");
    script.setAttribute("src", "app.dart");
  }
  document.getElementsByTagName("head")[0].appendChild(script);
</script>

References:

[1]Dynamically loading an external JavaScript or CSS file

[2]Detect language unavailability - Synonyms - Dart, JavaScript, C#, Python


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK