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

javascript - External JS file causing my webpage to appear blank - Stack Overflow

programmeradmin2浏览0评论

I'm making a browser-based clone of an old strategy game called Diplomacy. This is what I've got so far:

<html>

<head>

<title>Diplomacy</title>
<script type="text/javascript" src="game.js" />

</head>


<body>

<canvas id="canvas" width="750" height="750" style="border: 3px solid black; background:url('DiplomacyMap.png');">
Your browser does not support Canvas.
</canvas>

</body>

</html>

What does it look like in-browser? A blank page. However, if I remove the link to the external JS file in the head (game.js), it shows up no problem. This doesn't appear to have to do with the contents of the JS file, as even if I delete them or ment them all out, the problem persists. It seems to be to do with the mere presence of the element.

If I replace the <script src="file.js"> element with an embedded <script> ...code... </script>, the page works fine.

So... what's going on?

PART 2:

<script type="text/javascript">
colorProvince(PARIS, FRANCE);
</script>

Although the colorProvince function and the PARIS and FRANCE constants are all defined in game.js, this function isn't being called (as verified by a simple alert() in the colorProvince function). This doesn't do anything, either:

<script type="text/javascript">
alert(colorProvince);
</script>

I'm making a browser-based clone of an old strategy game called Diplomacy. This is what I've got so far:

<html>

<head>

<title>Diplomacy</title>
<script type="text/javascript" src="game.js" />

</head>


<body>

<canvas id="canvas" width="750" height="750" style="border: 3px solid black; background:url('DiplomacyMap.png');">
Your browser does not support Canvas.
</canvas>

</body>

</html>

What does it look like in-browser? A blank page. However, if I remove the link to the external JS file in the head (game.js), it shows up no problem. This doesn't appear to have to do with the contents of the JS file, as even if I delete them or ment them all out, the problem persists. It seems to be to do with the mere presence of the element.

If I replace the <script src="file.js"> element with an embedded <script> ...code... </script>, the page works fine.

So... what's going on?

PART 2:

<script type="text/javascript">
colorProvince(PARIS, FRANCE);
</script>

Although the colorProvince function and the PARIS and FRANCE constants are all defined in game.js, this function isn't being called (as verified by a simple alert() in the colorProvince function). This doesn't do anything, either:

<script type="text/javascript">
alert(colorProvince);
</script>
Share Improve this question edited Jun 27, 2011 at 17:26 bpeterson76 12.9k6 gold badges50 silver badges82 bronze badges asked Jun 27, 2011 at 16:44 Jack MJack M 6,1757 gold badges53 silver badges77 bronze badges 1
  • The 2nd part really should be a separate question. Also without seeing how game.js is defined it's going to be difficult to answer. – David Ly Commented Jun 27, 2011 at 17:27
Add a ment  | 

2 Answers 2

Reset to default 9
<script type="text/javascript" src="game.js" />

needs to be

<script type="text/javascript" src="game.js"></script>

and in game.js there is an extra semicolon in colorProvince(). replace it with this:

function colorProvince(province, nation){
    alert(5);
    for (var i = 0; i < province.getRectangles().length; i++)
    {
        //colorRect(rect, "red");
    }
}

<script> tags can not be self closed. They have to have an ending tag, otherwise most browsers will choke.

Instead of <script /> Use <script></script>

发布评论

评论列表(0)

  1. 暂无评论