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