最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript not loading in browser - Stack Overflow

programmeradmin1浏览0评论

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 puter. 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);
}

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 puter. 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);
}
Share Improve this question asked Feb 17, 2017 at 0:38 Alberto ArriagaAlberto Arriaga 111 gold badge1 silver badge2 bronze badges 2
  • This is the exact code, right? – Umair Khan Commented Feb 17, 2017 at 0:42
  • any errors on console? – Ajay Narain Mathur Commented Feb 17, 2017 at 0:42
Add a ment  | 

3 Answers 3

Reset to default 2

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>

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();

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!

发布评论

评论列表(0)

  1. 暂无评论