Javascript equivalent of #include or @import?

W

Weston C

I know of course that you can use <script src=""></script> in an HTML
document to include javascript code you don't want to spill all over the
page.

I'm wondering if there's a way to include other pieces of javascript
code from *within* javascript.... a la #include in C, @import in Java,
use/require in Perl, include()/require() in PHP, etc....

Thanks,
Weston

~==~
http://weston.canncentral.org/
weston8[at]cann8central.org
(remove eights to email me)
 
L

Lasse Reichstein Nielsen

Weston C said:
I know of course that you can use <script src=""></script> in an HTML
document to include javascript code you don't want to spill all over the
page.

Not understood. Are you talking about
<!-- <script src="..." type="text/javascript"></script> -->
or
<script src="..." type="text/javascript"><!-- --></script>
?
The first does nothing, the second doesn't need the comment.
I'm wondering if there's a way to include other pieces of javascript
code from *within* javascript.... a la #include in C, @import in Java,
use/require in Perl, include()/require() in PHP, etc....

Not in general, no. If you want to include it while the page is being
loaded, you can use document.write to write a new Javascript tag:

document.write('<script type="text/javascript src="..."></script>');

After the page has loaded, it becomes harder. Some browsers allow you
to load a script by adding a new script tag to the page using DOM methods,
or by changing the src attribute of an existing tag, or by changing the
content of an existing script tag.
Not all browsers understand all, or any, of these methods, though,
so it's not something to depend on.

/L
 
L

Lasse Reichstein Nielsen

Do you know of a browser that doesn't support dynamically loading a script
file? Other than O7?

I'm running O7.5 preview, and it works there.
I can do:

var scr = document.createElement("script");
scr.type="text/javascript";
document.body.appendChild(scr);
scr.src = "../../alert.js"; // alerts something to show it has loaded.

and also changing the text of a script element:

document.getElementById("idOfScriptElement").text = "alert('foo')"

You'll just have to wait for it to be released before it becomes
widely used :)

(Please write what it is a link to. I usually don't follow links like
this without a description).
I am still beating my head with Opera in general without resorting
to an IFrames/Frames solution. But knowledge of any browser that
won't handle the methods in the above URL would be of great use to
me.

That article looks very thorough. I am not familiar with browsers
outside of Netscape, Mozilla/Gecko, IE and Opera (but of those, I have
17 versions installed :). I'll assume that Netscape 2 and 3 won't work
either, nor earlier Operas, but I don't really care enough to test :)

/L
 
W

Weston C

Thought:

http://jibbering.com/2002/4/httprequest.html

So, to import a javascript file from inside of javascript:

function include(url)
{
xmlhttp.open("GET", url,true);
xmlhttp.onreadystatechange=function() {
eval(xmlhttp.responseText);
}
xmlhttp.send()
}

I haven't tried this out yet; xmlhttp does not appear to work with
Camino on Mac OS X (Gecko based) so I'm not sure what its coverage is,
and I don't know exactly how it works.
But this looks like a good lead to me...

~==~
http://weston.canncentral.org/
weston8[at]cann8central.org
(remove eights to email me)
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Not in general, no. If you want to include it while the page is being
loaded, you can use document.write to write a new Javascript tag:

document.write('<script type="text/javascript src="..."></script>');

The missing delimiter for the "type" attribute aside, this will
firstly definitely[tm] break in NN4 due to the NRLB[1], secondly
break in for-this-point SGML-compliant parsers (finding the ETAGO
delimiter "</" as end of the "script" element it needs to be
enclosed in, thus passing an unterminated string to the script
engine), and is thirdly not required at all if the inclusion is
unconditional. Instead, a simple

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

suffices in the latter case and will do no harm to noscript users.
If conditional inclusion is required, the best bet is

<script type="text/javascript">
<!--
document.write('<script type="text/javascript" src="..."><\/script>');
//-->
</script>

but the NRLB issue is nevertheless (in)valid, so you better not count
on it. Instead, one should either find a way to write that "script"
element dynamically server-side or disregard all NN4 users (but the
latter is not a wise decision for a commercial site).


PointedEars
___________
[1] Netscape Run-Length Bug: Non-reproducible bug making
NN4 throwing away/overwriting parts of the source code.
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lasse Reichstein Nielsen wrote:
Not in general, no. If you want to include it while the page is being
loaded, you can use document.write to write a new Javascript tag:

document.write('<script type="text/javascript src="..."></script>');

The missing delimiter for the "type" attribute aside,
Bummer!

this will firstly definitely[tm] break in NN4 due to the NRLB[1],

That one I was not aware of. It's worth worrying about if one wants
to support Netscape 4.
secondly break in for-this-point SGML-compliant parsers (finding the
ETAGO delimiter "</" as end of the "script" element it needs to be
enclosed in, thus passing an unterminated string to the script
engine),

That was deliberate. I did not write the surrounding script tags,
and in that case, I don't assume by default that the code is embedded
in HTML/XHTML. So, the code is plain Javascript and will work if
included in an external javascript file.
and is thirdly not required at all if the inclusion is
unconditional.

To solve the original poster's problem, you can't depend on HTML.
The inclusions should be made by the Javascript (presumably so
you can change the inclusion without changing existing pages).

At that, it is probably not as good a solution as could be hoped for,
because the declarations of the included page will not be immediately
available to the file loading it, only to subsequent script elements,
or code running after the included file has been parsed.

suffices in the latter case and will do no harm to noscript users.
If conditional inclusion is required, the best bet is

<script type="text/javascript">
<!--
document.write('<script type="text/javascript" src="..."><\/script>');
//-->
</script>

but the NRLB issue is nevertheless (in)valid, so you better not count
on it.

I'll look into it.
/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Thomas 'PointedEars' Lahn said:
Lasse said:
Not in general, no. If you want to include it while the page is being
loaded, you can use document.write to write a new Javascript tag:

document.write('<script type="text/javascript src="..."></script>');

[...]
secondly break in for-this-point SGML-compliant parsers (finding the
ETAGO delimiter "</" as end of the "script" element it needs to be
enclosed in, thus passing an unterminated string to the script
engine),

That was deliberate. I did not write the surrounding script tags,
and in that case, I don't assume by default that the code is embedded
in HTML/XHTML.

ACK, though it is better to mention the ETAGO issue to prevent
the unexperienced from running into trouble on this.
To solve the original poster's problem, you can't depend on HTML.

| I'm wondering if there's a way to include other pieces of javascript
| code from *within* javascript.... a la #include in C, @import in Java,
| use/require in Perl, include()/require() in PHP, etc....

You can.
The inclusions should be made by the Javascript (presumably so
you can change the inclusion without changing existing pages).

That would not be only of academical value, while causing problems with
dynamically written "script" elements in practice. (No, I do not mean
only the NRLB.)
At that, it is probably not as good a solution as could be hoped for,
because the declarations of the included page
Page?

will not be immediately available to the file loading it, only to
subsequent script elements, or code running after the included file
has been parsed.

Which is exactly the nature of the pendants in other languages mentioned
above.
I wouldn't add <!-- and //-->. I prefer to write correct ECMAScript,
and there is no current browser that requires them.

Depends on what you call "current browser". Besides,
there is no "current browser" that supports ECMAScript.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lasse said:
Thomas 'PointedEars' Lahn said:
Lasse Reichstein Nielsen wrote:
Not in general, no. If you want to include it while the page is being
loaded, you can use document.write to write a new Javascript tag:

document.write('<script type="text/javascript src="..."></script>');

[...]
secondly break in for-this-point SGML-compliant parsers (finding the
ETAGO delimiter "</" as end of the "script" element it needs to be
enclosed in, thus passing an unterminated string to the script
engine),

That was deliberate. I did not write the surrounding script tags,
and in that case, I don't assume by default that the code is embedded
in HTML/XHTML.

ACK, though it is better to mention the ETAGO issue to prevent
the unexperienced from running into trouble on this.

It is reasonable. I shall consider wrapping in script tags when
appropriate.

| I'm wondering if there's a way to include other pieces of javascript
| code from *within* javascript.... a la #include in C, @import in Java,
| use/require in Perl, include()/require() in PHP, etc....

You can.

Is there a method that works consistently across modern browsers?
(rules out XMLHTTP, but probably not iframes).
That would not be only of academical value, while causing problems with
dynamically written "script" elements in practice. (No, I do not mean
only the NRLB.)

What other problems are there with dynamically written script elements?

Script. Doh.
Which is exactly the nature of the pendants in other languages mentioned
above.

No, when they have an include statement at the top of a script, the
included material is available to the remainder of that script. Using
document.write of a script tag will only make the included material
available to subsequent script tags, not the remainder of the
execution of the one contining the document.write.
Depends on what you call "current browser". Besides,

Anything more recent than Netscape 3?
there is no "current browser" that supports ECMAScript.

Probably correct. The ones I have tested all fail on this script:
<script type="text/javascript">
var z = 1;
if ( 0 <!-- z
) { alert(42); }
</script>
It should execute alert(42) according to the ECMAScript standard.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Probably correct. The ones I have tested all fail on this script:
<script type="text/javascript">
var z = 1;
if ( 0 <!-- z
) { alert(42); }
</script>
It should execute alert(42) according to the ECMAScript standard.

That code works in <URL:http://www.merlyn.demon.co.uk/js-quick.htm>,
which uses eval in a fully-loaded page, in my MSIE4.

It also works in a being-loaded page if converted to a string parameter
to eval.

It does nothing in a being-loaded page if inserted as published.

AISB, eval has its uses <g>.
 
S

seven

Rats. I'm trying to script in PhotoShop, which uses Javascript, and
it sure is a waste to have to copy code around instead of just
including it. I guess it's a limitation of not seeing all potential
uses of a language before hand. Oh well.

PhotoShop? Did you mean DreamWeaver?



---
http://seven.postmodern.com
seven at post modern dot com
If sending an email, +add+ the word
"newsgroup" to the subject line to
temporarily avert the spam filter.
 
S

Simon Wigzell

To load any file into the current page from the server use :
<!--#include file="[filename]"-->This is server side code.Javascript runs on
the client so unless everyone who is going to visit your website has the
java files on their computers, what you want would not work!"gendem"
 
S

Simon Wigzell

I use the following to include any file on the server :

<!--#include file="yourfilenamehere.js"-->

I'm not sure what support you need to run it. I use ASP for my server side
programming but this
isn't asp.
Anyway, try it, if your server has even minimal support it should work.
Remember, this is happening on the server. Javascript runs on the client. I
fail to see how
using javascript document.read or write is going to work unless everyone who
visits your web page has the
files on their computers!
 
R

Randy Webb

Simon said:
To load any file into the current page from the server use :
<!--#include file="[filename]"-->This is server side code.Javascript runs on
the client so unless everyone who is going to visit your website has the
java files on their computers, what you want would not work!

Before offering javascript advice, one should learn the difference
between java and javascript.

Before posting to a usenet newsgroup, one should read its FAQ.
 
S

Simon Wigzell

Oh please pardon me for my typo - I said "java" not "javascript" in my
second use of the term. Do you disagree with the substance of my post? Is
there a word for shits like you who go around trying to prove their
superiority by picking on typos in other peoples posts? I've been
programming for 15 years thanks very much. I'm trying to help someone, what
are you doing you miserable maggot?


Randy Webb said:
Simon said:
To load any file into the current page from the server use :
<!--#include file="[filename]"-->This is server side code.Javascript runs on
the client so unless everyone who is going to visit your website has the
java files on their computers, what you want would not work!

Before offering javascript advice, one should learn the difference
between java and javascript.

Before posting to a usenet newsgroup, one should read its FAQ.
 
R

Randy Webb

Simon said:
Simon Wigzell wrote:

To load any file into the current page from the server use :
<!--#include file="[filename]"-->This is server side code.Javascript

runs on
Before offering javascript advice, one should learn the difference
between java and javascript.

Before posting to a usenet newsgroup, one should read its FAQ.
Oh please pardon me for my typo - I said "java" not "javascript" in my
second use of the term.

If you consider java a typo for javascript.........

javasrcipt is a typo, java instead of javascript is not.
Do you disagree with the substance of my post?

Since you talked about "java files", and the question was about
javascript then yeah I disagree with you. As for changing it to
javascript instead of java, then I still disagree with the "substance"
because it offers no solution to the problem, not even close.
Is there a word for shits like you who go around trying to prove their
superiority by picking on typos in other peoples posts? I've been
programming for 15 years thanks very much.

You have been programming 15 years and don't know the difference between
java and javascript? (other than to try to pass it off as a typo?).
I'm trying to help someone, what are you doing you miserable maggot?

Correcting incompetent, unknowledgable idiots like you.
 
L

Lasse Reichstein Nielsen

gendem said:
The scripting I do has no server; Adobe has implemeneted javascript as
a cross-platform scripting language to control Photoshop CS, it works
in much the same way as vb and applescript do in this case. There is
no server, no browser, nothing to do with the web at all, sorry if I
wasn't clear.

Ah, yes. The power of a completely abstract language ...
SSI and the other techniques work for web pages, but not for anything
else. The lack of an equivalent command to use/require in perl or
#include in C is what I'm looking for, and from the sounds of it, that
doesn't exist.

.... along with the penalties for not assuming anything about the
context it runs in. Since ECMAScript is completely agnostic about the
environment it runs in, it can't have an include method build in. That
would require making assumptions about, e.g., files and directories,
in order to work.

So, it must be the job of the host environment to make include options
available. Browsers haven't typically done that in Javascript itself,
because it is possible to go throug HTML or the DOM. In your case, you
don't have that possibility.

If they try to make documents self-contained, then it won't make sense
to allow dynamic includes of external scripts. Then your document
wouldn't work if mailed to someone else, unless you also include the
external file. For, e.g., PDF, that would be a problem. So, maybe
there is no way, but you should ask some Adobe specialists, because it
is not a general Javascript problem.
/L
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top