Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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

0
newbieweb
Asked:
newbieweb
3 Solutions
 
enachemcCommented:
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 NatarajanLAMP DeveloperCommented:
you make an sample.js file and include it in the files?
0
 
MulithCommented:
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
 
newbiewebAuthor Commented:
Thanks!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now