Get the source (code) of an external script?

Can I get the contents of an external script as a string? Something equivalent myInlineScript.textContent?

The scenario is that I am just starting to log into WebGL and all the tutorials that I find in the repository shaders as inline tags <script type="x-shader/x-foo">. However, the element itself is not important, shaders are created from simple strings. They are easily extracted from the built-in scripts via .textContent, but of course I prefer that my code be separated from my markup.

Is there any equivalent to getting the source / contents of an external script? A quick crawl of DOM documents and a Google search did nothing.

Update

WebGL shaders are not a big problem - there are several ways to get strings in Javascript! But this is not the only time I have encountered script tags that were used for embedded non-scripts , and he was curious how to do this better.

+5
source share
1 answer

If it is in the same domain, you can simply execute a normal ajax request for it and return the file as a string. This works for any text file.

, WebGL, , , "x-shader" script - . , -. .

+2

All Articles