'Javascript not loading in browser

Im a beginner programmer and im experiencing some issues loading javascript code on my browser. Are there any errors in my code? or could it be an issue with my computer. If it helps: im using OS Sierra, Google Chrome, Atom editor.

Thanks!!

HTML code:

<!DOCTYPE html>

<html>
    <head>

    </head>

    <body>
        <title>A* Algorithm</title>

            <h1>This is the A* pathfinding algorithm</h1>
            <script src ="astar.js"> </script>

    </body>


</html>

Javascript code:

var cols = 5;
var rows = 5;
var grid = new Array(cols);

function setup() {
    createCanvas(400, 400);
    console.log("A*");

    for (var i = 0; i < cols; i++) {
        grid[i] = new Array(rows);
    }

    for (var i = 0; i < cols; i++) {
        for (var j = 0; i < row; j++) {
            grid[i][j] = new Spot();
        }
    }

    console.log(grid);
}

function draw () {
    background(0);
}


Solution 1:[1]

You have to call this Javascript function somewhere in any event there are plenty of ways, here the simplest one. However, additionally you haven`t defined the createCanvas function. hope it helps

<!DOCTYPE html>
<html>
<head>
    <title>A* Algorithm</title>
</head>
<body onload="setup()">
    <h1>This is the A* pathfinding algorithm</h1>
    <script src="astar.js"> </script>
</body>
</html>

Solution 2:[2]

There is nothing wrong with your code, given that you script is named correctly and is in the same directory as you HTML file.

The cause, however, may be that you never call neither of you methods, you just construct them.

Simply including the script is not enough for the methods to run. To execute the function setup simply add the following code to the end of your current script.

setup();

Solution 3:[3]

Steps:

  1. Make sure your javascript and the html file is in the same folder. I notice that the part to your file (src ="astar.js") is the same as the name of your file.

  2. Check the javascript console in the browser. One of the most effective ways to program is learning from your error messages. Javascript error messages can be reached view>>Developer>>Javascript console. When you are in the javascript console, run the code again and check for errors and go from there.

Cheers!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Faheem Ahmad
Solution 2 OptimusCrime
Solution 3