简体   繁体   English

Javascript不会在github页面网站上运行

[英]Javascript won't run on github pages site

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. 因此,javascript曾经在我的github页面网站上工作,但是在我删除存储库并试图通过一些更改重新上传项目后它不再起作用了。 Here is the repo . 这是回购 Here is the site . 这是网站

You are serving the site over HTTPS but trying to load jQuery over HTTP. 您通过HTTPS服务该站点,但尝试通过HTTP加载jQuery。 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. https替换http ,你应该好好去。 This error is easily discoverable if you open the Javascript Console in your browser (usually under F12). 如果您在浏览器中打开Javascript控制台(通常在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. 拼出https://cdnjs.cloudflare.com/更好。

Here's why: 原因如下:

Some advice from Paul Irish , one of developers behind Chrome: Chrome开发人员之一Paul Irish的一些建议

If the asset you need is available on SSL, then always use the https:// asset. 如果您需要的资产在SSL上可用,则始终使用https:// asset。

It's always safe to request HTTPS assets even if your site is on HTTP, however the reverse is not true. 即使您的站点在HTTP上,请求HTTPS资产也始终是安全的,但反之则不然。

当您使用非指定的 http protocols ,最好不要将它们放在任何请求中......所以,而不是http://仅使用//

 console.log("$", window.jQuery) 
 <script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM