How can I reference a JavaScript file from GitHub in my HTML file?

911 views Asked by At

How can I refer to a JavaScript file from GitHub in an HTML file? I have a JavaScript file. I need to upload it to GitHub and use it in my HTML files also I even edited the JavaScript file it will work in my HTML files with with the latest modification.

I tried to use

<script src="https://[myusername].github.io/[myrepository]/[filename].js"></script>
 script src="https://raw.githubusercontent.com/[myrepository]/[filename].js"></script>

it did not work then i tried the folowing:

<script src="https://cdn.jsdelivr.net/gh/<user>/<repo>/<filename>.js">

, which works, but doesn't load the latest modification of my JavaScript file.

1

There are 1 answers

0
starball On

It should be https://raw.githubusercontent.com/[username/organization-name]/[repository name]/[branch name]/[path to file].

cdn.jsdelivr.net is a CDN. CDNs usually implement some sort of caching. I wouldn't be surprised if you just need to wait for the caching period to pass to see your changes.