So the javascript used to work on my github pages site but it doesn't work anymore after I delete the repository and tried to re-uplaod the project with some changes. Here is the repo. Here is the site.
Asked
Active
Viewed 2.3k times
4
-
I should also mention that Jekyll runs the site locally just fine. – Austin Magnuson Aug 09 '16 at 13:57
-
I should also mention that the suggestions below did not work in index.html. – Austin Magnuson Aug 09 '16 at 21:30
-
I managed to get everything working by creating a new index.js file. Thanks again to everyone that gave feedback. – Austin Magnuson Aug 10 '16 at 00:10
-
Repo and site are 404. This isn't a helpful question :( – Matt Joiner Aug 22 '23 at 11:27
2 Answers
21
You are serving the site over HTTPS but trying to load jQuery over HTTP. This is not allowed.
<!-- wrong -->
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
Replace http
with https
and you should be good to go. This error is easily discoverable if you open the Javascript Console in your browser (usually under F12).
<!-- correct -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
But what about protocol relative links?
You could also use this once-popular syntax:
<!-- meh -->
<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
but it's cargo cult at this point. Spelling out https://cdnjs.cloudflare.com/
is better.
Here's why:
- Your CDN offers HTTPS! That's wonderful. Make sure to use it. Even if your site is delivered via plain http (but why?) your users can still at least get the assets securely.
- The page will still work if you open it locally via a
file://
URI, which is sometimes helpful during development. - The performance impact is negligible and you have no reason to worry about it.
Some advice from Paul Irish, one of developers behind Chrome:
If the asset you need is available on SSL, then always use the
https://
asset.It’s always safe to request HTTPS assets even if your site is on HTTP, however the reverse is not true.
-
2The answer by @Hitmands is much more protocol agnostic and works better (can serve both `HTTP` and `HTTPS`) – StephenG Aug 09 '16 at 15:34
-
@StephenG in what way does it work better? see my comment on that answer. What am I missing? – Kos Aug 09 '16 at 15:54
-
1It follows the protocol that is used for the serving site, so if `HTTP` is used to serve the main page, `HTTP` will be used to serve the javascript. Same for `HTTPS` By making it protocol agnostic, the site can react to being served over different protocols without code changes – StephenG Aug 09 '16 at 15:57
-
1@StephenG why is that good or helpful for CDN code? if you always ask for https, everything works just as well + you're sure you really download the code from `cdnjs.cloudflare.com`. Admit it, you just think `//` looks neater :-) – Kos Aug 09 '16 at 19:01
-
Edited my answer and added some references. See also http://stackoverflow.com/questions/28446314/why-use-protocol-relative-urls-at-all – Kos Aug 09 '16 at 19:15
-
So I actually got it working on every page except for the index.html. Thank you guys so much for the feedback it has been a huge help. – Austin Magnuson Aug 09 '16 at 21:28
-
Trying to set the security of the transport/session layer (SSL/TLS) through an application layer modification (HTTP) doesn't make sense. A HTTP served page with HTTPS assets can just as easily be MITM to served HTTP assets, or completely different content. The security of the original page is what matter. That sets the security of all of the subsequent assets. It's also not always safe to request HTTPS assets over HTTP, depending on network configurations. A user might not be able to get SSL/TLS sessions because of firewalls, breaking the page – StephenG Aug 09 '16 at 23:45
3
when you work with non specified http protocols
could be a good idea don't put them in any request... so, instead of http://
use only //
.
console.log("$", window.jQuery)
<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

Hitmands
- 13,491
- 4
- 34
- 69
-
3I intentionally avoid this syntax. If the CDN offers `https` then you should use it even if your site is on HTTP. Also minor nitpick - protocol agnostic paths don't work with `file://` – Kos Aug 09 '16 at 15:53