Solved

How do I load from an outside javascript file?

Posted on 2010-11-30
4
290 Views
Last Modified: 2012-05-10
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
Comment
Question by:newbieweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 150 total points
ID: 34237534
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
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 150 total points
ID: 34237535
you make an sample.js file and include it in the files?
0
 
LVL 1

Assisted Solution

by:Mulith
Mulith earned 200 total points
ID: 34237546
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
 

Author Closing Comment

by:newbieweb
ID: 34237551
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question