billven billven - 2 months ago 6
HTML Question

Call javascript from HTML

I have a HTML file with the content:

<script src="http://spelprogrammering.nu/simple.js">
function test()
{
//Function stuff
}
</script>


However, I'd like to write all my javascript (Function test) in a separate document (.js). How do I refer to, or call, this separate file so I get the same result as if the code was directly in the HTML?

I need the http://spelprogrammering.nu/simple.js to ease the graphics handling in function test.

Answer

In the html section, refer to the file that contains your javascript, I'm assuming the html file is in the same folder as that containing your script.

<script src="script.js"></script>

If the script is in a different folder...

<script src="/path/to/script.js"></script>
Comments