Github js文件未加载(直接link)
Github js file not loading (direct link)
我通过在页面上的 <head>
标签中放置 <script>
标签,为 public 创建了一个编码库,但是当我尝试使用 a 函数时,它说undefined
当它 运行.
。我将 url 链接到 index.js
文件,但它不会将其加载到客户端用户。
<head>
<script src="https://github.com/McJoe21/coderslib/blob/master/index.js"></script>
</head>
当我 运行 我在 index.js
文件中定义的 console.log(ranInteger(1,10))
时,我得到了 ranInteger is not defined
错误。欢迎大家的帮助!
您尝试导入的文件是文件的 GitHub 查看器。要导入该文件的原始数据,您需要使用此代码:
<script>
const source = 'https://raw.githubusercontent.com/McJoe21/coderslib/master/index.js';
fetch(source).then((response) => {
return response.text()
}).then((response) => {
eval(response);
}).catch((error) => {
console.error(`An error occured while attempting to load the "${source}" resource!`);
console.error(error);
});
</script>
由于 GitHub 不允许直接导入其用户内容,因此您需要将源 URL 包装在上面的 fetch-eval 块中。
从技术上讲,GitHub 不允许像 CDN 一样从他们的站点访问源代码,但是从 This Whosebug Question 开始,有一个解决方法。我不建议使用它,但您可以使用“https://cdn.jsdelivr.net/gh/”让您的脚本正常工作(来自 Whosebug 上的用户@anayarojo)。
你的 url 看起来像这样:
https://cdn.jsdelivr.net/gh/McJoe21/coderslib/index.js
URL 的模式是:
https://cdn.jsdelivr.net/gh/<username>/<repository>/<file>
我通过在页面上的 <head>
标签中放置 <script>
标签,为 public 创建了一个编码库,但是当我尝试使用 a 函数时,它说undefined
当它 运行.
。我将 url 链接到 index.js
文件,但它不会将其加载到客户端用户。
<head>
<script src="https://github.com/McJoe21/coderslib/blob/master/index.js"></script>
</head>
当我 运行 我在 index.js
文件中定义的 console.log(ranInteger(1,10))
时,我得到了 ranInteger is not defined
错误。欢迎大家的帮助!
您尝试导入的文件是文件的 GitHub 查看器。要导入该文件的原始数据,您需要使用此代码:
<script>
const source = 'https://raw.githubusercontent.com/McJoe21/coderslib/master/index.js';
fetch(source).then((response) => {
return response.text()
}).then((response) => {
eval(response);
}).catch((error) => {
console.error(`An error occured while attempting to load the "${source}" resource!`);
console.error(error);
});
</script>
由于 GitHub 不允许直接导入其用户内容,因此您需要将源 URL 包装在上面的 fetch-eval 块中。
从技术上讲,GitHub 不允许像 CDN 一样从他们的站点访问源代码,但是从 This Whosebug Question 开始,有一个解决方法。我不建议使用它,但您可以使用“https://cdn.jsdelivr.net/gh/”让您的脚本正常工作(来自 Whosebug 上的用户@anayarojo)。
你的 url 看起来像这样:
https://cdn.jsdelivr.net/gh/McJoe21/coderslib/index.js
URL 的模式是:
https://cdn.jsdelivr.net/gh/<username>/<repository>/<file>