Saturday, 23 April 2016

Calling a script from an external file

We Can just recall the previous example of image switching to understand how to call script externally.
 Let's have a look.
 
<html>
<head>

</head>
<body>

click on the image

</body> </html>
Using the tag
  <script type="text/javascript" src="functions.js"> </script> 
we can call the script externally. Always write script in file with extension ".js".
functions.js:
 
function imageswitch(){
var image = document.getElementById('switchimage');
if(image.src.match("one"))
{
image.src = "flower_two.jpg";
}
else
{
image.src = "flower_one.jpg";
}
}
Note: Make sure that the both html and js file locations are the same or else defime them specifically

No comments:

Post a Comment