Javascript won't run on github pages site

ぐ巨炮叔叔 提交于 2019-12-30 07:13:20

问题


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.


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/38852819/javascript-wont-run-on-github-pages-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!