1

I'm trying to include a separate .js file so I can pull in a class I created but it doesn't work, can someone tell me why?

playercharacter.js

function PlayerCharacter() {
this.x = 0; 
this.y = 0;

this.dx = 5;
this.dy = 5;
}


PlayerCharacter.prototype.sayHello = function()
{
 alert ('hello');
};

index.html

<html>
<head>
<script src="js/playercharacter.js" type="text/javascript" ></script>
</head>
<body>
 <script type="text/javascript" >
var player = new PlayerCharacter();
player.sayHello();
</script>
</body>
</html>
7
  • 1
    Put an alert() outside any function in "playercharacter.js" to make sure that it's being loaded at all. Commented May 14, 2013 at 0:41
  • Why does this code sample start with "PSEUDO"? It doesn't appear to be pseudocode. Commented May 14, 2013 at 0:56
  • I put an alert in, and you're right - it's not executing? I checked the file paths - indexh.html is in root and js file is in a fodler called js ... Commented May 14, 2013 at 1:10
  • What in the world, I uploaded this example as a zip file...doesn't get any simpler than this, so why doesn't it work? ryandebraal.com/HTML5_test3.zip Commented May 14, 2013 at 1:18
  • Yes, it's definitely advisable to set the path to the correct fodler. Commented May 14, 2013 at 1:18

3 Answers 3

1

You forgot wrap your JS-code into the <script> tags

<html>
 <head>
  <script src="js/playercharacter.js" type="text/javascript" ></script>
 </head>
 <body>
  <script type="text/javascript" >
   var player = new PlayerCharacter();
   player.sayHello();
  </script>
 </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Your JavaScript code is not in a script tag.

<script>
    var player = new PlayerCharacter();
    player.sayHello();
</script>

Comments

0

You need to surround the code in the body with a <script> tag.

2 Comments

Mobile SO doesn't notify of new answers.
Ok ;) Didin't know that hehe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.