Javascript onload callback for "type" or "rel", "text / plain" in script or link tag

Why doesn't onload load for script type = "text / plain"? loadPlain below does not call a callback, but loadScript does.

I expected this to work ... right?

loadPlain("http://localhost/ajax/libs/jquery/1.10.2/jquery.min.js", function(element) { alert(1); alert(element.innerHTML); }, undefined, {}) function loadPlain(path, callback, errorCallback, options) { var element = document.createElement('script'); element.setAttribute("type", 'text/plain'); element.setAttribute("src", path); return loadBase(element, callback, errorCallback, options); } function loadScript(path, callback, errorCallback, options) { var element = document.createElement('script'); element.setAttribute("type", 'text/javascript'); element.setAttribute("src", path); return loadBase(element, callback, errorCallback, options); } function loadBase(element, callback, errorCallback, options) { element.loaded = false; if (element.readyState){ // IE element.onreadystatechange = function(){ if (element.readyState == "loaded" || element.readyState == "complete"){ element.onreadystatechange = null; loadBaseOnload(element, callback); } }; } else { // Others element.onload = function() { loadBaseOnload(element, callback); }; } element.onerror = function() { errorCallback && errorCallback(element); }; (options.elementAppendTo || document.head || loadBase.head || (loadBase.head = document.getElementsByTagName('head')[0]) || document.body).appendChild(element); return element; } function loadBaseOnload(element, callback) { if (element.loaded != true) { element.loaded = true; if ( callback ) callback(element); } } 

Please note that I know about XMLHttpRequest, but this is not a question :)

+4
source share
2 answers

WHATWG (an organization that defines browser behavior, along with W3C) has a list of well-known script MIME types and some blacklisted MIME types that should not be considered scripting languages:

The following are strings of the MIME type that should be recognized by user agents and the languages ​​to which they refer:

  • "application/ecmascript"
  • "application/javascript"
  • "application/x-ecmascript"
  • ...

The following MIME types (with or without parameters) should not be interpreted as scripting languages:

  • "text/plain"
  • "text/xml"
  • "application/octet-stream"
  • "application/xml"

Note. These types are explicitly listed here because they are poorly defined types, which, however, can be used as formats for data blocks, and it would be problematic if they were suddenly interpreted by the user as a script by the user agent.

That the WHATWG specification calls “data blocks” here are non-scripts enclosed in <script> tags:

This example uses two script elements. An external script is embedded, and the other is some data.

 <script src="game-engine.js"></script> <script type="text/x-game-map">` ........U.........e o............A....e .....A.....AAA....e .A..AAA...AAAAA...e </script> 

The components of the WHATWG specification that indicate load events for <script> tags explicitly indicate that they run scripts that are referenced by <script> tags, and not script data blocks. A <script> element is a data block if its type not recognized as a MIME type corresponding to the scripting language supported by the browser. This means that blacklisted types, such as text/plain , will never be recognized as scripts, while in some browsers type values ​​can be supported neither in the support list, nor in support, nor in support, like application/dart (for Google Dart).

In addition, the inclusion of a non-script type along with src not spec-compliant. Data blocks are legal only if they are indicated in the line:

When used to include data blocks (unlike scripts), data must be embedded in a string , the data format must be specified using the type attribute, src must not be specified , and the contents of the script element must meet the requirements defined for the format used.

+8
source

If you specify your script as text / plain, the browser cannot do anything with it.

You must specify it as "script / javascript" so that it runs as JavaScript.

+1
source

Source: https://habr.com/ru/post/1495322/


All Articles