How do I load from an outside javascript file?

I am new to JavaScript and found this sample program.  I'd like to change it to load the alert from an outside .js file, but am not successful. Please post each working file and provide the filename to use for each.

Thanks,
newbieweb
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>

<body onload="message()">
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>

</html>

Open in new window

newbiewebSr. Software EngineerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
enachemcConnect With a Mentor Commented:
make another .js file and just define the alerts as variables
include this .js in the head before all other js
you can now use the variable anywhere in the page
alert(message_var_from_js)
0
 
Loganathan NatarajanConnect With a Mentor LAMP DeveloperCommented:
you make an sample.js file and include it in the files?
0
 
MulithConnect With a Mentor Commented:
Create a file called something like script.js

and insert the javascript without the script tags like this
function message()
{
alert("This alert box was called with the onload event");
}

Open in new window


the add the following line of text in your html:

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

If you save the external javascript file in the same directory as the page you are loading it will retrieve the file correctly.
0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.