2

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.

3
  • But your script loads there normally what it does is that it produces error becuase you can't run script working with DOM in the head Commented Mar 20, 2019 at 7:13
  • Did it fix that problem? Commented Mar 20, 2019 at 9:50
  • Yup it did! ,Thanks so much, Commented Mar 20, 2019 at 9:56

2 Answers 2

2

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()">
Sign up to request clarification or add additional context in comments.

Comments

0

Use the raw js file path in your HTML:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.