Reputation: 185
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.
Upvotes: 2
Views: 2538
Reputation: 2537
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()">
Upvotes: 2
Reputation: 2209
Use the raw js file path in your HTML:
<script src="https://raw.githubusercontent.com/awsomeguy563/awsomeguy563.github.io/master/code.js"></script>
Upvotes: 0