Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to call a function which created in other JS files

Status
Not open for further replies.

jimmyweb

Programmer
Dec 9, 2004
37
US
how to call a javascript function which created in other JS files
Hi Friends,
I created 2 javascript piece. one link to target js file. one is online script. in second JavaScript, i want to call a function that created in JS files.
I got a error message and can not find list name--undifine function name which created in fristcode.js. which is wrong?
below is my codes.
Thanks for any help.
jim
*******************
<html>
<head><title> test</title>
<script language="javascript" type="text/javascript" SRC="//firstcode.js define a list function
</script>');

<script language="javascript" type="text/javascript">//
// i call list function below for an instance
var newList = null;
newList = new List();
</script>;
</head>
<body>
some code here to call second Javascript
</body>
</html>
 
what you're doing is correct - just make sure that firstcode.js defines the function exactly as you try to use it, e.g. "List()"

also make sure not to include <script> or </script> tags inside the actual file firstcode.js - just javascript only

it could also be possible that firstcode.js has not finished downloading before you call for List()...you could add a global var to the end of firstcode.js to represent it being loaded, like
var firstcodeLoaded = true;

then don't try to call List() unless firstcodeLoaded = true, e.g.

<script language="javascript" type="text/javascript">

var newList = null;

function makeList() {
if (firstcodeLoaded) {
newList = new List();
}
else {
setTimeout("makeList();", 100);
}
}

makeList();

</script>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Dear Jeff,
you are right.the firstcode is not loaded.but how to force to load firstcode?
based on your email, i modify codes. it seems i not correctly declare globe var. also alert no loading message does not display after testing.
please check my codes.
Thanks for your times.
jim
******

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title> test</title>
<script type="text/javascript" SRC="<!--
var firstcodeLoaded = true;
//firstcode.js define a function list
// -->

</script>

<script type="text/javascript" >
<!--
function kk(){
alert("AA");
if (firstcodeLoaded){
alert("bb");
var NewList = null;
NewList = new List();
alert("gg");
}
else {
alert("no loading");
setTimeout("List();", 100);
}

}
// -->
</script>
</head>
<body>
<p>some code here to call second Javascript---<br>
<input type="button" onclick="kk()">
</body>
</html>
 
Jeff wrote:


also make sure not to include <script> or </script> tags inside the actual file firstcode.js - just javascript only


but you did that in your second example anyway. Get rid of what's in red below COMPLETELY:

<script type="text/javascript" SRC="[red]
<!--
var firstcodeLoaded = true;
//firstcode.js define a function list
// -->[/red]
</script>

Lee
 
I was wrong about what Jeff told you. However, you should not have anything in the <script> tags that load an external JS file.

Lee
 
your file firstcode.js should look something like:
Code:
<!-- hide js from weak browsers
 //  nothing but javascript code in here
function List() {
  //  define the List function here
  //  ...
}

//  end of file, set the loaded flag
var firstcodeLoaded = true;

// end hiding js from weak browsers -->

and your html page:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title> test</title>
<script  type="text/javascript" SRC="[URL unfurl="true"]http://homepage/clientview/firstcode.js"><script[/URL] type="text/javascript"  >
<!--
var NewList = null;

function loadList() {
 if (firstcodeLoaded){
  NewList = new List();
  alert("loaded");
 }
 else {
  alert("not loaded, trying again");
  setTimeout("loadList();", 100);
 }
}

function kk() {
 if (!NewList) {
  alert("NewList not initialized, calling loadList()");
  loadList();
 }
 else {
  alert("NewList OK");
 }
}
// -->
</script>
</head>
<body>
<p>some code here to call second Javascript---<br>
<input type="button" onclick="kk()">
</body>
</html>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top