External JavaScript

Hello, I'm currently working on a HTML file where I link an external JavaScript file so that I don't have to keep writing out large functions within the HTML itself. I'm using the <script> tags but when I run the HTML it doesn't seem to be executing the function form the JavaScript, here is a simplified example to demonstrate what I mean:

HTML:
[ICODE]
<html>
<head>
<title>Test</title>
</head>

<body>


<h1>This is a test</h1>

// Importing JavaScript file (I've checked that the file path is correct)
<script type="text/javascript" src="LargeFunctions.js">

// Attempting to use a JavaScript function created in the imported file
externalTest();

</script>


</body>

</html>

[/ICODE]

JavaScript:

[ICODE]
function externalTest()
{
alert("If this message is displayed then the javascript file has been successfully linked to the html");
}
[/ICODE]

As you can see the externalTest() function should display alert text as soon as I load the web page, but instead it just does nothing, I don't understand why this would be the case, so if anyone could help me out that would be great. Thanks in advance.

Comments

  • It's possible/likely that the script is not loaded yet when the browser hits the function call. Try moving your script into the head tag.
Sign In or Register to comment.