How to use Include Files

M

mtek

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
 
B

Bart Van der Donck

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).

Hope this helps,
 
M

mtek

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).

Hope this helps,


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
 
T

Thomas 'PointedEars' Lahn

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
 
M

mtek

(e-mail address removed) 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).
[...]

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

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

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- Hide quoted text -

- Show quoted text -


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??
 
T

Thomas 'PointedEars' Lahn


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

PointedEars
 
B

Bart Van der Donck

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top