1

I was following the steps and wrote a demo HTML. Here are the steps in the README of https://github.com/commonmark/commonmark.js#commonmarkjs :

For client-side use, you can do make dist to produce a standalone JavaScript file js/dist/commonmark.js, suitable for linking into a web page, or fetch the latest from https://raw.githubusercontent.com/jgm/commonmark.js/master/dist/commonmark.js, or bower install commonmark.

Here is my demo 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>

Here is a JSFiddle URL for my demo: https://jsfiddle.net/y3xohp7x/

I saved this HTML locally in a file named foo.html and opened this local file with Firefox 55.0.1.

But if I load it with Firefox 55.0.1, I get the following errors in the console.

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

Questions:

  1. Why does this error occur?
  2. How can I resolve this error without having to copy commonmark.js to the local filesystem?
  3. Is it a bug in the commonmark.js README documentation that I quoted above or is it an error in my understanding of the documentation?
Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Lone Learner
  • 18,088
  • 20
  • 102
  • 200

1 Answers1

0

Edit

Actually, there apparently is a way to do this for github content in particular: https://rawgit.com/

This website gives you a link to a version of the script that will be served with the correct MIME-type.

So this should load the script properly for you:

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

https://jsfiddle.net/w1uvq59r/


Original Answer

The reason is that the script source is sent back with the headers:

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

The latter header prevents the browser from executing the script due to the fact that the content-type is not executable, like "application/javascript". Which means unfortunately there is really no way to get the script to load remotely. Here is a thread with more information on a similar problem.


The only solution, as far as I can tell, is to load it locally, like so:

<script type="application/javascript" src="path/to/the/file.js"></script>
Michael Horn
  • 3,819
  • 9
  • 22
  • Can you answer all the three questions I have asked? – Lone Learner Aug 16 '17 at 02:42
  • I edited my comment to hopefully add some clarity. May I ask why you are not able to use the script locally? – Michael Horn Aug 16 '17 at 03:07
  • There is no particular reason to avoid using the script locally. I am just exploring the possibilities for the sake of learning. For example, if I can link directly to the GitHub URL of the script, then I don't have to worry about updating my local copy of script periodically. – Lone Learner Aug 16 '17 at 04:18