JavaScript不会在github页面上运行 [英] Javascript won't run on github pages site
问题描述
您通过HTTPS提供站点服务,但尝试通过HTTP加载jQuery。这是不允许的。
<! - wrong - >
< script src ='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'>< / script>
用 http
> https ,你应该很好去。如果您在浏览器中打开Javascript控制台(通常在F12下),该错误很容易被发现。
<! - correct - >
< script src ='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'>< / script>
但是协议相关链接怎么样?
您也可以使用这种曾经流行的语法:
<! - - meh - >
< script src ='// cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
但在这一点上它是货真价实。拼出 https://cdnjs.cloudflare.com/
更好。
原因如下:
- 您的CDN提供HTTPS!那好极了。确保使用它。即使您的网站通过普通的http (但为什么?) 提供,您的用户至少仍可以安全地获取资产。
- 如果您通过
file://
URI在本地打开该页面,该页面仍然有效,这在开发过程中有时会很有帮助。 / li>
- 性能影响可以忽略不计,您没有理由担心。
来自Paul Irish的一些建议,Chrome背后的开发人员之一: b
$ b
如果您需要的资产可用SSL,然后始终使用
https://
资产。
请求HTTPS资产总是安全的,即使您的网站在HTTP上,但反过来却是不正确的。
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.
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.
这篇关于JavaScript不会在github页面上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!