简体   繁体   English

是否可以在GitHub页面的HTML文件上加载JavaScript代码?

[英]Is it possible to load javascript code on your HTML file on GitHub pages?

This is my GitHub pages site at https://awsomeguy563.github.io/ 这是我的GitHub页面站点,位于https://awsomeguy563.github.io/

The sit runs on this repo: https://github.com/awsomeguy563/awsomeguy563.github.io 坐姿在此仓库上运行: https : //github.com/awsomeguy563/awsomeguy563.github.io

And I have a local repository on my laptop that is a clone of this remote repository. 我的笔记本电脑上有一个本地存储库,它是此远程存储库的副本。

I want to be able to display my javascript code on the website, but nothing works. 我希望能够在网站上显示我的JavaScript代码,但没有任何效果。 Can someone recommend me the easiest way to do this? 有人可以推荐我最简单的方法吗? The javascript code is on the repository. javascript代码位于存储库中。 And I have a local repository on my laptop. 我的笔记本电脑上有一个本地存储库。

The javascript code is code.js on the repository above. javascript代码是上述存储库中的code.js。

Your code.js is there and it's loaded. 您的code.js在那里,并且已加载。 What you are doing wrong is that you are loading that script in head tag so it executes before the body is loaded and that produces that error. 您做错的是您正在将该script加载到head标记中,因此该script在加载正文之前执行,并且会产生该错误。

To solve it you can move the script under closing body tag. 为了解决这个问题,您可以将script移动到关闭body标签下。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Life</title>
</head>
<body bgcolor="#14FFB0">
<canvas id="myCanvas" width="800" height="600" style="border:1px solid #000000;"></canvas>
</body>
<script src="code.js"></script>
</html>

Or you can keep the script tag where is it and you can load the function onLoad 或者,您可以将script标签保留在原处,然后可以将函数加载到onLoad

function runOnLoad() {
   var c = document.getElementById("myCanvas");
   var ctx = c.getContext("2d");
   ctx.clearRect(0,0,800,600);
   ctx.fillStyle = "red";
   ctx.fillRect(10,10,100,100);
}

And in html just update body tag 并在html中更新主体标签

<body bgcolor="#14FFB0" onload="runOnLoad()">

在HTML中使用原始js文件路径:

<script src="https://raw.githubusercontent.com/awsomeguy563/awsomeguy563.github.io/master/code.js"></script>

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

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