javascripthtmlgithubgithub-pagescdn

Raw Github js file not loading (direct link) like CDN


I created a coding library for the public to use by putting a <script> tag in the <head> tag on the page but when I try to use the a function it says undefined when it ran.

. I linked the url to the index.js file but it doesn't load it to the client user.

<head>
<script src="https://github.com/<username>/<repo>/blob/master/index.js"></script>
</head>

When I run a console.log(ranInteger(1,10)) which I have defined in my index.js file but I get a ranInteger is not defined error. All help is welcome!


Solution

  • Technically speaking, GitHub doesn't allow source code to be accessed from their site like a CDN, however from This Stack Overflow Question, there is a workaround. I wouldn't recommend using it, but you can use "https://cdn.jsdelivr.net/gh/" to get your script to work (from user @anayarojo on Stack Overflow).

    The url in your case would look like this:

    https://cdn.jsdelivr.net/gh/McJoe21/coderslib/index.js
    

    The pattern for the URL is: https://cdn.jsdelivr.net/gh/<username>/<repository>/<file>