# Simple javascript ?

**URL:** https://boards.straightdope.com/t/simple-javascript/245979
**Category:** Factual Questions
**Created:** [May 19, 2004, 3:28pm UTC](https://boards.straightdope.com/t/simple-javascript/245979 "2004-05-19T15:28:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![enipla](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@enipla](https://boards.straightdope.com/u/enipla)
#### Post date: [May 19, 2004, 3:28pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/1 "2004-05-19T15:28:45Z")

</div>

Real simple. All I want to be able to do is access variables and functions set in other .js files. Call them dependent files.

Where do you define the ‘dependent files’?

I thought it was like this -

\<script language=“javascript” SRC=“ThisIsADependentFile.js” TYPE=“text/javascript”\>

It’s driving me nuts. I have looked though both of my javascript books and they don’t mention anything about it.

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [May 19, 2004, 3:35pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/2 "2004-05-19T15:35:19Z")

</div>

I’m not sure that you need the type in there.

But you DO need the closing \</script\> tag in there.

\<SCRIPT language=“JavaScript” SRC=“ThisIsADependentFile.js”\>\</SCRIPT\>

It’s called “external file” by the way, if you want to google some on it.

Oh, and make sure the path is correct. Putting that on a page has you looking for a file in the same directory as the page you put the call on.

---

<div class="post-metadata">

### Author: ![enipla](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@enipla](https://boards.straightdope.com/u/enipla)
#### Post date: [May 19, 2004, 3:42pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/3 "2004-05-19T15:42:06Z")

</div>

Yep, I’ve got the end \</script\> Tag in my code. And the correct path.

Neither of my books have a reference for ‘external files’. :mad:

I’ll google.

---

<div class="post-metadata">

### Author: ![Duckster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duckster/32/1244_2.png) [@Duckster](https://boards.straightdope.com/u/Duckster)
#### Post date: [May 19, 2004, 3:53pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/4 "2004-05-19T15:53:09Z")

</div>

[Using an external javascript.](http://www.pageresource.com/jscript/jxtern.htm)

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [May 19, 2004, 4:18pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/5 "2004-05-19T16:18:49Z")

</div>

How do you know that the problem is in the script call? What error are you getting? Maybe the error is in one of the funcs…?

---

<div class="post-metadata">

### Author: ![enipla](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@enipla](https://boards.straightdope.com/u/enipla)
#### Post date: [May 19, 2004, 4:30pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/6 "2004-05-19T16:30:15Z")

</div>

Got it. Thanks everyone. Zipper, I guess you where right.

This works -

\<script language=“javascript” SRC=“[http://someserver/javascript/aimsXML.js](http://someserver/javascript/aimsXML.js)”\>  
\</script\>  
\<script\>  
testfunction();   
\</script\>

This does not -

\<script language=“javascript” SRC=“[http://someserver/javascript/aimsXML.js](http://someserver/javascript/aimsXML.js)”\>  
testfunction();   
\</script\>

---

<div class="post-metadata">

### Author: ![Duckster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duckster/32/1244_2.png) [@Duckster](https://boards.straightdope.com/u/Duckster)
#### Post date: [May 19, 2004, 5:21pm UTC](https://boards.straightdope.com/t/simple-javascript/245979/7 "2004-05-19T17:21:46Z")

</div>

> [@enipla](#):
>
> Got it. Thanks everyone. Zipper, I guess you where right.
> 
> This works -
> 
> \<script language=“javascript” SRC=“[http://someserver/javascript/aimsXML.js](http://someserver/javascript/aimsXML.js)”\>  
> \</script\>  
> \<script\>  
> testfunction();   
> \</script\>

It works because the first script references the master javascript where all functions are located. The second script is the actual function you are using on the web page from the master javascript.

In other words, you could have this on a web page:  
\<head\>  
\<script\>  
Function 1

Function 2

Function 3  
\</script\>  
\</head\>

While in the body you may have:  
\<body\>

Content  
\<script\>  
Function 2  
\</script\>  
content  
\<script\>  
Function 3  
\</script\>  
Content  
\</body\>

A different web page using the same functions might only use Function 1 on the page. You avoid having to duplicate all the functions on every web page by creating an external Javascript master document and reference it on every web page. Then, you only use the functions relevant to the page in question. If you choose to add more javascript code, you only add it to the master javascript _and_ select the correct functions for that page.

Clear as mud?

> [@](#):
>
> This does not -
> 
> \<script language=“javascript” SRC=“[http://someserver/javascript/aimsXML.js](http://someserver/javascript/aimsXML.js)”\>  
> testfunction();   
> \</script\>

It doesn’t work because you are attempting to do two things at once.

---

<div class="post-metadata">

### Author: ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)
#### Post date: [May 20, 2004, 3:29am UTC](https://boards.straightdope.com/t/simple-javascript/245979/8 "2004-05-20T03:29:43Z")

</div>

> [@enipla](#):
>
> This does not -
> 
> \<script language=“javascript” SRC=“[http://someserver/javascript/aimsXML.js](http://someserver/javascript/aimsXML.js)”\>  
> testfunction();   
> \</script\>

More specifically, this fails becasue the browser either loads the SRC= file , OR processes the “testfunction()’” code, BUT NOT BOTH. Which is processed is browser dependent, but at least in IE 6, the SRC= overrides and so your testfunction() never gets to be part of the page.

Either way, as you say, put them in separate \<script\>\</script\> tagged blocks & you’ll be A-OK.
