Refused to execute script

javascript
github

#1

I created a gist on GitHub with some javascript on it and typed this code to add it to my HTML page.

 <script src="https://gist.github.com/Ptolemy2002/7ad9e685da3c80750363347c70529b86#file-artist-js"></script>

When I previewed my page, I found this error on the console:

Refused to execute script from 'https://gist.github.com/Ptolemy2002/7ad9e685da3c80750363347c70529b86#file-artist-js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

I made sure that the gist was public and disabled all cookie blocking. My full workspace is here:

https://ide.c9.io/ptolemy2002/canvas-library

(look inside of “Editor.html”)


#2

This is the link to the Gist page, which is designed to be pretty and to have tools for editing the gist. You cannot import the code using this link, since it is HTML, as the error describes. Use the link that opens when you click the “Raw” button at the top of the gist file. In this case, it’s https://gist.githubusercontent.com/Ptolemy2002/7ad9e685da3c80750363347c70529b86/raw/0f6fa03df00e29269ec3452886e50dde2f3f8dbc/Artist.js.

EDIT: Also, if this is something you’ll be putting into production or will be used a lot, you’ll want to self-host the script, as GitHub will block you from using that script and may take more severe measures if they notice large amounts of traffic to that file.


#3

I will probably want to do that. But how do I self-host the script?


#4

Based on your code, I’m not exactly sure how you would plan to host your site. Your cheapest and easiest option would probably be GitHub pages, and in that case, you would simple include the Artist.js file in the repository as it is currently (I would highly recommend this method if you haven’t decided on anything yet). If you’re using something like Apache, it will probably be just as easy, just having the JS file in the same folder or in a javascript folder that is accessible at either example.com/Artist.js or example.com/javascript/Artist.js, respectively. Self-hosting the JavaScript alongside the rest of your files is usually pretty simple, my point is just that you need to have permission to host it (GitHub pages I believe explicitly provides permissions in the GitHub ToS, but just linking directly to a Gist would be a violation since you’re using GitHub’s bandwidth for your own site, thereby making them pay more for a service that is not optimized for providing content. Good luck!