0

我按照这些步骤编写了一个演示 HTML。以下是https://github.com/commonmark/commonmark.js#commonmarkjs README 中的步骤:

对于客户端使用,您可以make dist生成一个独立的 JavaScript 文件js/dist/commonmark.js,适合链接到网页,或者从 https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark获取最新的.jsbower install commonmark.

这是我的演示 HTML。

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <script src="https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark.js"></script>
      <script>
         window.onload = function() {
             console.log(commonmark)
         }
      </script>
   <body></body>
</html>

这是我的演示的 JSFiddle URL:https ://jsfiddle.net/y3xohp7x/

我将这个 HTML 保存在一个名为的文件中foo.html,并用 Firefox 55.0.1 打开了这个本地文件。

但是如果我使用 Firefox 55.0.1 加载它,我会在控制台中收到以下错误。

Loading failed for the <script> with source “https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark.js”. foo.html:5
ReferenceError: commonmark is not defined foo.html:9:5

问题:

  1. 为什么会出现这个错误?
  2. 如何在无需复制commonmark.js到本地文件系统的情况下解决此错误?
  3. 这是我上面引用的 commonmark.js README 文档中的错误,还是我对文档的理解有误?
4

1 回答 1

0

编辑

实际上,显然有一种方法可以特别针对 github 内容执行此操作:https ://rawgit.com/

该网站为您提供了一个脚本版本的链接,该版本将使用正确的 MIME 类型提供服务。

所以这应该为您正确加载脚本:

<script type="application/javascript" src="https://cdn.rawgit.com/jgm/commonmark.js/master/dist/commonmark.js"></script>

https://jsfiddle.net/w1uvq59r/


原始答案

原因是脚本源与标头一起发回:

"Content-Type": "text/plain"
"X-Content-Type-Options": "nosniff"

后一个标头阻止浏览器执行脚本,因为内容类型不可执行,例如“application/javascript”。这意味着不幸的是,实际上没有办法让脚本远程加载。这是一个线程,其中包含有关类似问题的更多信息。


据我所知,唯一的解决方案是在本地加载它,如下所示:

<script type="application/javascript" src="path/to/the/file.js"></script>
于 2017-08-16T02:27:51.740 回答