(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 -