Scripts
can also be placed in external files. Use external JavaScript files when you
want to use the same script on many pages, but don't want to have to rewrite
the code on every page!
Example
1– How to create external javaScript file ‘myScript’
|
<script
type="text/javascript">
function
todaydate(){
var today_date= new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth()+1
var mytoday=today_date.getDate()
document.write(myyear+"/"+mymonth+"/"+mytoday)
}
</script>
|
How to
Use External JavaScript File
Step 1: Open up your text editor (such as notepad),
type out the above code, and save it as an individual file with the extension .js (like
this: myScript.js).
|
<script src="
myScript.js"type="text/javascript"></script>
|
Step 2: On all pages that use the above library,
create a reference to it by using the below code. It consist of a
<script> tag with the optional src property included inside:
External
scripts are practical when the same code is used in many different web
pages.JavaScript files have the file extension .js.To use an external script,
put the name of the script file in the src (source) attribute of a
<script> tag.
Example
2:-how to include external javascript file in webpage
|
<!DOCTYPE
html>
<html>
<body>
<script
src="myScript.js"></script>
</body>
</html>
|