Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including JavaScript files from GitHub into HTML pages

PAY ATTENTION!

You can't include Github scripts directly from Github after this change.

We added the X-Content-Type-Options: nosniff header to our raw URL responses way back in 2011 as a first step in combating hotlinking. This has the effect of forcing the browser to treat content in accordance with the Content-Type header. That means that when we set Content-Type: text/plain for raw views of files, the browser will refuse to treat that file as JavaScript or CSS.

But there are alternatives. Check my answer to this question.


I am trying to include a JavaScript file from GitHub into a local HTML file for testing (like an image found on the internet: <img src="http://...">).

I would like something like this:

<script src="https://github.com/[username]/[repository]/blob/master/public/[fileName].js"></script> 

The problem is that this doesn't work.

How can I do this?

like image 789
Ionică Bizău Avatar asked Dec 25 '12 19:12

Ionică Bizău


People also ask

How do I include a JavaScript file in an HTML file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can you include JavaScript in HTML?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

Can you have JavaScript on GitHub Pages?

GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.

Can you use GitHub for HTML?

Wherever HTML is rendered on GitHub (gists, README files in repos, comments on issues and pull requests, ...) you can use any of the HTML elements that GitHub Flavored Markdown (GFM) provides syntactic sugar for.


2 Answers

You will be able to do it with a URL similar to this:

https://rawgit.com/h5bp/html5-boilerplate/master/src/js/plugins.js 

Note that this is not the same as clicking on the "raw" button within GitHub; that button will also give you a clean version of the file, but it will be sent with the wrong headers.


A Word of warning; the file is not not being served from GitHub. It is being redirected through the rawgit.com domain. As is stated on https://rawgit.com:

Hey! rawgit.com is just for fun and is not associated with GitHub in any way.

Keep in mind that the owner of that domain is now in control of the traffic and is able to manipulate it as they see fit.

like image 144
Lix Avatar answered Sep 28 '22 08:09

Lix


After enabling GitHub pages of your Repository, use following link:

<script src="https://[username].github.io/[repository]/[filename].js"></script> 
like image 36
Chintan Patel Avatar answered Sep 28 '22 08:09

Chintan Patel