如何访问通过< script type =" text / plain"检索的纯文本内容SRC = ...>在JavaScript? [英] How to access plain text content retrieved via <script type="text/plain" src=...> in JavaScript?

查看:965
本文介绍了如何访问通过< script type =" text / plain"检索的纯文本内容SRC = ...>在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用< script type =text / plainsrc =http:// ...>< / script> 时,其中的网址是指纯文本文件,有没有办法在JavaScript中访问文件的内容?该文件将传输到浏览器,但脚本元素的 innerHTML 属性的值不会更改(它仍然是空字符串)。检查DOM中的元素节点似乎没有显示可以通过其找到收到的内容的任何属性。

When using <script type="text/plain" src="http://..."></script>, where the URL refers to a plain text file, is there a way to access the content of the file in JavaScript? The file is transferred to the browser, but the value of innerHTML property of the script element is not changed (it remains the empty string). Inspecting the element node in the DOM does not seem to reveal any property through which the content received could be found.

我知道可以使用XMLHTTPRequest代替,但我'我对浏览器以我描述的方式获取数据但似乎没有提供任何访问权限的问题感兴趣。

I know that XMLHTTPRequest can be used instead, but I’m interested in the problem why browsers fetch data in the way I described but do not seem to offer any access to it.

推荐答案

首先, HTMLScriptElement text 属性是访问内联<$ c的文本的首选方法$ c>< script> 元素。 DOM-Level- 2 HTML5:4.11.1 都表明脚本应该有一个属性 text ,其中包含脚本内部 text

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:


IDL属性 text 必须返回作为子项的所有Text节点的内容的串联脚本元素(忽略任何其他节点,如注释或元素),按树顺序。在设置时,它必须以与 textContent IDL属性相同的方式运行。

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.

由于< script> 元素为空(您指定了外部源), text textContent innerHTML 为空。这是因为 text 属性仅在内联脚本中设置:

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:


文本的值元素的已启动标志最后一次设置时的IDL属性是脚本源。

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


因此,使用此方法无法包含外部 text / plain

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

参见还有:

  • W3C: HTML5: 4.11.1 The script element: text attribute and the example for the game map:

<script src="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>



这篇关于如何访问通过&lt; script type =&quot; text / plain&quot;检索的纯文本内容SRC = ...&GT;在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆