1

I tried to write more that one function in an external .js file and it was not working. So i wrote the javascript inside the html body by using tag. cant we write more than 1 function outside? coz only the 1st function used to work well. Where as the rest did not work. I'm a fresher

suhas
  • 127
  • 3
  • 3
  • 11
  • 3
    Yes. Of course. After all, all "external functions" are really just side-effects of polluting the global (`window`) namespace in some fashion. So, anyway .. to get help on SO, make sure to present the *actual* problem/question with the *actual* relevant code and the *actual* error messages or failure indicators. –  Jan 08 '13 at 06:04
  • maybe your dom content of the page did not load properly before the Javascript function tried to access it. for help regarding this refer to previous post http://stackoverflow.com/questions/65434/getting-notified-when-the-page-dom-has-loaded-but-before-window-onload – AurA Jan 08 '13 at 06:06
  • I'm not aware of the dom components as im doin javascript since a week. anyways thanks.. – suhas Jan 08 '13 at 06:11
  • Can you post the code here? the whole js file (as sample) if possible! you should not have ` – Rookie Programmer Aravind Jan 08 '13 at 13:21

1 Answers1

8

Yes you can write multiple function into external java script file.i am posting here one small sample. 1. create test.js file inside one folder named as js 2. create test.html file

1. test.html

insert following line into test.html file

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/test.js"></script> 
</head>
<body>
<a href="#" onclick="test()">Click Here to call first function</a><br>
<a href="#" onclick="test1()">Click Here to call second function</a>  
</body>
</html>
  1. test.js

Insert following code into your external java script file which name is test.js

function test(){
    alert("fist function is called");

}
function test1(){
    alert("second function is called");
}
Nitesh Singh Rajput
  • 607
  • 1
  • 7
  • 24