Solved

How do I load from an outside javascript file?

Posted on 2010-11-30
4
291 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

628 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