Javascript文件可以在计算机文件上使用HTML,但不能在Github上使用

Javascript file works with HTML on computer file but not on Github

本文关键字:文件 但不能 Github HTML Javascript 计算机      更新时间:2023-09-26

我有一个通过github页面的个人网站,我的最后一个问题是我有一份使用jQuery的javascript文件,我在html文件的head中链接到它,如下所示。。。

<head>    
    <script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script type="text/javascript" src="smoothscroll.js"></script>
    <link rel="stylesheet" href="mystyle.css">
</head>

当我在自己的计算机上测试文件中的代码时,脚本、CSS和HTML一起工作,但当我将它们上传到github时,只有我的HTML和CSS文件工作,而不是我的JS。

它们都位于存储库的同一分支中。

你知道为什么js文件不起作用吗?

您会注意到您的Github页面是通过https提供的。当使用https协议托管网站时,通过不安全协议加载的任何资源都将被阻止。这就是为什么您的jQuery不会加载或运行。

尝试将http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js更改为https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js

您需要将您的头更改为以下内容:

<head>    
    <script type="text/javascript"  src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script type="text/javascript" src="smoothscroll.js"></script>
    <link rel="stylesheet" href="mystyle.css">
</head>

当您不确定使用哪种协议访问该页面时(称为协议不可知),并且您包含了来自另一个支持这两种协议的域的资源时,应使用//来代替https和/或httpDomains domains ports and protocols must match

相关文章: