Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use Include Files

6 views
Skip to first unread message

mt...@mtekusa.com

unread,
Mar 24, 2008, 11:01:46 PM3/24/08
to

Does Javascript support include files? I have some functions that I
need in 4 pages with Javascript. Can I somehow include that data by
using a general include file? All of the Javascript is not the same,
just some functions. So, I really have only one set to <SCRIPT>.....</
SCRIPT>.

Much of that code is unique to the page, but some of the functions are
re-usable. Any way to include the reusable code.


Thanks!


John

Bart Van der Donck

unread,
Mar 25, 2008, 6:55:17 AM3/25/08
to
m...@mtekusa.com wrote:

The normal way is to put those functions in a separate .js-call:

<script type="text/javascript" src="remote.js"></script>

Alternatively, you could add a script dynamically to the head-section
of the page:

var sc = document.createElement('script');
sc.type = 'text/javascript';
sc.src = 'remote.js';
document.getElementsByTagName('head')[0].appendChild(sc);

Then there are various methods to load external data; like
XMLHttpRequest or (hidden) iframe. But this needs an eval-command to
execute the code (which is generally to avoid).

Hope this helps,

--
Bart

mt...@mtekusa.com

unread,
Mar 25, 2008, 7:36:39 PM3/25/08
to


Bart,

One thing I am unclear on is this....Say I have the following in one
page:

<SCRIPT>
function {
.
.
.
}

some code which calls the above function

</SCRIPT>

Then, I have this in the other page:

<SCRIPT>
function {
.
.
.
}

some differnet code which calls the above function

</SCRIPT>

Since the function is reusable, how do I include it between the
<SCRIPT> ... </SCRIPT> headers??

Thanks!

John

Thomas 'PointedEars' Lahn

unread,
Mar 25, 2008, 7:50:56 PM3/25/08
to
mt...@mtekusa.com wrote:
> On Mar 25, 5:55 am, Bart Van der Donck <b...@nijlen.com> wrote:
>> m...@mtekusa.com wrote:
>>> Does Javascript support include files? I have some functions that I
>>> need in 4 pages with Javascript. Can I somehow include that data by
>>> using a general include file? All of the Javascript is not the same,
>>> just some functions. So, I really have only one set to <SCRIPT>.....</
>>> SCRIPT>.
>>> Much of that code is unique to the page, but some of the functions are
>>> re-usable. Any way to include the reusable code.
>> The normal way is to put those functions in a separate .js-call:
>>
>> <script type="text/javascript" src="remote.js"></script>
>>
>> Alternatively, you could add a script dynamically to the head-section
>> of the page:
>>
>> var sc = document.createElement('script');
>> sc.type = 'text/javascript';
>> sc.src = 'remote.js';
>> document.getElementsByTagName('head')[0].appendChild(sc);
>>
>> Then there are various methods to load external data; like
>> XMLHttpRequest or (hidden) iframe. But this needs an eval-command to
>> execute the code (which is generally to avoid).
>> [...]
>
> Bart,

This is NetNews, not e-mail.

> One thing I am unclear on is this....Say I have the following in one
> page:

That is not a page, it is an HTML document fragment at best; and an invalid
one, since the `script' element requires the `type' attribute.

http://validator.w3.org/

> <SCRIPT>
> function {
> .
> .
> .
> }
>
> some code which calls the above function
>
> </SCRIPT>
>
> Then, I have this in the other page:

Which "other page" are you talking about?

> <SCRIPT>
> function {
> .
> .
> .
> }
>
> some differnet code which calls the above function
>
> </SCRIPT>
>
> Since the function is reusable, how do I include it between the
> <SCRIPT> ... </SCRIPT> headers??

Those are not headers, they are tags (start tag and end tag) which along
with the content in-between make up a `script' element.

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.1

Either you have your function declared in an external script resource which
you would refer to with the `src' property here. (That is the recommended
approach here.)

Or you would append a text node to contain the script code. Some UAs also
provide a `text' property but there is no guarantee that this will work either.

It is best not to rely on such dirty hacks, and include the `script' element
as-is. For maximum flexibility, a server-side script can take care of that.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Message has been deleted

Thomas 'PointedEars' Lahn

unread,
Mar 26, 2008, 3:23:58 AM3/26/08
to
mt...@mtekusa.com wrote:
> [...] Thomas 'PointedEars' Lahn [...] wrote:
>> [full quote]

For the last time, please trim your quotes:

http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Trim

> I guess what I am really asking here is that I see this:


>
> <script type="text/javascript" src="remote.js"></script>
>

> Now, if that is one 'script', can I also use another set of tags:
> <SCRIPT> - </SCRIPT> and refer to the functions I just included??

Yes, of course:

<script type="text/javascript" src="remote.js"></script>

<script type="text/javascript">
// call foo(), declared in remote.js
foo();
</script>

Why do you not just try before you ask? Why do you not look at code of
existing Web sites before you ask? Why do you not read previous discussions
before you ask?

http://catb.org/~esr/faqs/smart-questions.html


Score adjusted

Bart Van der Donck

unread,
Mar 26, 2008, 4:38:46 AM3/26/08
to
m...@mtekusa.com wrote:

> One thing I am unclear on is this....Say I have the following in one
> page:
>
> <SCRIPT>
> function {
> .
> .
> .
>
> }
>
> some code which calls the above function
>
> </SCRIPT>
>
> Then, I have this in the other page:
>
> <SCRIPT>
> function {
> .
> .
> .
>
> }
>
> some differnet code which calls the above function
>
> </SCRIPT>
>
> Since the function is reusable, how do I include it between the
> <SCRIPT> ... </SCRIPT> headers??

The reusable function needs to be placed in a separate file. Say 3
files in a same directory:

remote.js:

function showMessage(m) {
alert(m);
}

call from one.htm:

<script type="text/javascript" src="remote.js"></script>

<script type="text/javascript">
showMessage('Hello');
</script>

call from two.htm:

<script type="text/javascript" src="remote.js"></script>

<script type="text/javascript">
showMessage('Hello again');
</script>

Cheers,

--
Bart

0 new messages