How to access plain text content retrieved via in JavaScript?

First of all, the textattribute of the HTMLScriptElement is the preferred method to access the text of an inline <script> element. DOM-Level-2 and HTML5: 4.11.1 both indicate that a script should have an attribute text which contains the scripts interior text:

The IDL attribute text must return a concatenation of the contents of all the Text nodes that are children of the script element (ignoring any other nodes such as comments or elements), in tree order. On setting, it must act the same way as the textContent IDL attribute.

Since the <script> element is empty (you specified an external source), text, textContent and innerHTML are empty. This is because the text attribute is only set in inline scripts:

If the script is inline and the script block’s type is a text-based language:

The value of the text IDL attribute at the time the element’s “already started” flag was last set is the script source.

So it’s not possible to include an external text/plain using this method.

See also:

  • W3C: HTML5: 4.11.1 The script element: text attribute and the example for the game map:
    <script src="https://stackoverflow.com/questions/12760852/game-engine.js"></script> <!-- game engine isn't inline -->
    <script type="text/x-game-map"> <!-- but data needs to be inline -->
    ........U.........e
    o............A....e
    .....A.....AAA....e
    .A..AAA...AAAAA...e
    </script>
    

Leave a Comment