Client-side includes with JavaScript

G

Greg

I'm trying to simulate server-side includes on the client-side by
using JavaScript. My main problem is this: is there a way to somehow
pass a variable into a directive like this:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript"
src="$myPageVar$"></SCRIPT>


....where $myPageVar$ is a variable I've declared, and somehow,
magically, its value gets put into this directive? The only way I can
get this to work is if I hard-code a value for $myPageVar$ in the
following way:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript"
src="homeInc.js"></SCRIPT>


....which is of limited use. Is there a way for me to use variables in
such a directive? Or is there a better way to be doing client-side
includes?

Thanks.
 
K

kaeli

I'm trying to simulate server-side includes on the client-side by
using JavaScript. My main problem is this: is there a way to somehow
pass a variable into a directive like this:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript"
src="$myPageVar$"></SCRIPT>

This worked in IE...(put your file name where mine is for myVar)

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script>
myVar = "jsCaptureEnterKey.js";
document.write("<s"+"cript src='"+myVar+"'></s"+"cript>");
</script>
</head>

<body>
hi
</body>
</html>


-------------------------------------------------
~kaeli~
Why do people who know the least know it the loudest?
If that cell phone was up your a$$, maybe you could
drive a little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
D

Douglas Crockford

<head>
<title> New Document </title>
<script>
myVar = "jsCaptureEnterKey.js";
document.write("<s"+"cript src='"+myVar+"'></s"+"cript>");
</script>
</head>

The trick with writing script tags is that browsers get confused by scripts
containing "</" in theory and "</script>" in practice. The line above fails the
in theory case. Try this instead:

document.write('<script src="' + myVar + '"><\/script>');

Putting spaces around the plus signs make it much easier to read.

http://www.crockford.com/#javascript
 
L

Lasse Reichstein Nielsen

kaeli said:
This worked in IE...(put your file name where mine is for myVar)

Yes, the trick is to document.write the script tag with your
variables in place. (scripts of more scripts!).

<script type="text/javascript">

Just for being pedantic, the type attribute is mandatory in HTML 4,
and you did add the DOCTYPE (a bad DOCTYPE that puts browsers into
quirks mode, though, but that's another rant).
myVar = "jsCaptureEnterKey.js";
document.write("<s"+"cript src='"+myVar+"'></s"+"cript>");

Just write it as
document.write("<script src='"+myVar+"'><\/script>");

You don't need to split "script". The one thing that a script is not
allowed to contain (according to specification) is the sequence "</".
In practice, browsers allow that, and only ends the script tag at
"</script>". In either case, just one backslash is sufficient.

/L 'Just needed to say that!'
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
You don't need to split "script". The one thing that a script is not
allowed to contain (according to specification) is the sequence "</".
In practice, browsers allow that, and only ends the script tag at
"</script>". In either case, just one backslash is sufficient.

However, W3's TIDY, in checking mode, will give

Warning: '<' + '/' + letter not allowed here

for </bbb> in a test page containing

<script>
S = '</bbb>'
</script>

and it is useful to get a warning-free test even if the tester may be
over-strict.
 
K

kaeli

[email protected] enlightened us said:
Just for being pedantic, the type attribute is mandatory in HTML 4,
and you did add the DOCTYPE (a bad DOCTYPE that puts browsers into
quirks mode, though, but that's another rant).

Please explain.
My html editor sticks that in, and I can change it, so if it's a bad
one, I'd like to know about it.



Thanks for the tips, both of you who replied.

-------------------------------------------------
~kaeli~
Why do people who know the least know it the loudest?
If that cell phone was up your a$$, maybe you could
drive a little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
D

Douglas Crockford

You don't need to split "script". The one thing that a script is not
However, W3's TIDY, in checking mode, will give

Warning: '<' + '/' + letter not allowed here

for </bbb> in a test page containing

<script>
S = '</bbb>'
</script>

and it is useful to get a warning-free test even if the tester may be
over-strict.

jslint also checks for this.

http://www.crockford.com/javascript/lint.html
 
L

Lasse Reichstein Nielsen

[DOCTYPE]
Please explain.
My html editor sticks that in, and I can change it, so if it's a bad
one, I'd like to know about it.

Opera has an explanation of the DOCTYPE switching
<URL:http://www.opera.com/docs/specs/doctype/>
It behaves the same as IE and almost the same as Mozilla, and there
are also links to Microsoft and Mozilla pages on the same subject.

The short summary is that some DOCTYPEs puts browsers into
backwards compatability mode, where they emulate the bugs of IE4
(because a lot of pages were written for that browser and fails
completely in a purely standards compliant browser). New pages
should not be created for to backwards compatible mode, but to
standards mode.

Your DOCTYPE was for HTML 4.01 Transitional and with no URL, which
triggers quirks mode in all three browsers.

/L
 
K

kaeli

[email protected] enlightened us said:
Your DOCTYPE was for HTML 4.01 Transitional and with no URL, which
triggers quirks mode in all three browsers.

That is VERY useful to know.

Is it better to omit the doctype or should I use the proper one?


-------------------------------------------------
~kaeli~
Why do people who know the least know it the loudest?
If that cell phone was up your a$$, maybe you could
drive a little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
L

Lasse Reichstein Nielsen

kaeli said:
Is it better to omit the doctype or should I use the proper one?

Use a proper one. Omitting a doctype also triggers quirks mode.
/L
 
K

kaeli

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top