简体   繁体   中英

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/

The sit runs on this repo: 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. Can someone recommend me the easiest way to do this? The javascript code is on the repository. And I have a local repository on my laptop.

The javascript code is code.js on the repository above.

Your code.js is there and it's loaded. 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.

To solve it you can move the script under closing body tag.

<!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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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