Solved

How do I load from an outside javascript file?

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now