获取外部脚本的源(代码)? [英] Get the source (code) of an external script?
问题描述
可以获取外部脚本内容的字符串吗?等同于 myInlineScript.textContent
?
It is possible to obtain as a string the content of an external script? Something equivalent to myInlineScript.textContent
?
这种情况是我刚开始接触WebGL,在教程中,我发现存储着色器为内嵌< script type = x-shader / x-foo>
标签。元素本身并不重要,但是,着色器是由纯字符串创建的。这些很容易通过 .textContent
从内联脚本中提取,但是我当然希望将代码与标记分开。
The scenario is that I'm just starting to get into WebGL and all the tutorials I'm finding store shaders as inline <script type="x-shader/x-foo">
tags. The element itself isn't important, however — the shaders are created from plain strings. These are easily extracted from inline scripts via .textContent
, but I'd of course prefer to keep my code separate from my markup.
是否存在用于获取外部脚本的源代码/内容的等效项?快速浏览DOM文档和Google搜索并没有任何启发性。
Is there some equivalent for getting the source / content of an external script? A quick scan of the DOM docs and a Google search didn't yield anything enlightening.
更新
WebGL着色器不是一个很大的问题-有很多方法可以在Javascript中获取字符串!但这不是我唯一一次遇到脚本标记用于内联非脚本的情况,这很好奇
The WebGL shaders aren't a huge problem — there are a number of ways of getting strings in Javascript! But this isn't the only time I've encountered script tags being used to inline non-scripts and it got be curious about a better way to do it.
推荐答案
如果它在同一个域中,则可以对其进行常规的ajax请求,并以字符串形式返回文件。
If it's on the same domain you can just do a normal ajax request for it, and get the file back as a string. This works for any kind of text file.
此外,我对WebGL并不熟悉,但是如果您的着色器位于外部文件中,则假定使用 x-shader脚本类型只是将它们内联的一种方法。否则,它只是您传递给某个地方的方法的字符串。因此,不要对此考虑得太多。
Also, I am not familiar with WebGL, but if your shaders are in external files then I assume the "x-shader" script type is simply a way to put them inline. Otherwise, it's just a string you pass to a method somewhere. So don't over-think this too much.
这篇关于获取外部脚本的源(代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!