block writing

H

hendedav

Gang,

I have been searching for this on the web as best I can and have
found no answers. I am interested in using a document.write (or other
type of javascript code) to write a segment that contains wording and
xhtml code without escaping the code. For example:

document.write("this is an example of a link: <a href=\"test.html
\">link</a>");

would like to find a way to not have to escape the anchor tag,
maybe something like:

document.write[
this is an example of a link: <a href="test.html">link</a>
]

I know the square brackets will not work, just I am interested in
knowing if there is some way to delimit a start and end "tag" for the
write function so I don't have to escape any xhtml code. Thanks in
advance.

Dave
 
E

Erwin Moller

Gang,

I have been searching for this on the web as best I can and have
found no answers. I am interested in using a document.write (or other
type of javascript code) to write a segment that contains wording and
xhtml code without escaping the code. For example:

document.write("this is an example of a link: <a href=\"test.html
\">link</a>");

would like to find a way to not have to escape the anchor tag,
maybe something like:

document.write[
this is an example of a link: <a href="test.html">link</a>
]

I know the square brackets will not work, just I am interested in
knowing if there is some way to delimit a start and end "tag" for the
write function so I don't have to escape any xhtml code. Thanks in
advance.

Dave

Hi Dave,

Could it be a simple as using <pre>your content here</pre>?

And I advise using a div and replace its content instead of using
document.write, if that is an option for you.

Regards,
Erwin Moller
 
J

Joost Diepenmaat

Erwin Moller

It's easy if you use the DOM1 methods:

var element = document.getElementById("some id");
element.appendChild(document.createTextNode( said:
Hi Dave,

Could it be a simple as using <pre>your content here</pre>?

And I advise using a div and replace its content instead of using
document.write, if that is an option for you.

Me too.
 
H

hendedav

Erwin Moller



It's easy if you use the DOM1 methods:

var element = document.getElementById("some id");



Me too.


Thanks for the replies. Unfortunately in both of those instances, I
will still have to escape the xhtml portion. That is the part I am
looking at not having to do. The <pre> tags work for preformatted
text and wouldn't apply to changing or writing content in javascript.
The second example, I will still have to escape the xhtml code because
it falls between two quotes (""). Thanks again though. If anyone
else has any ideas, please let me know.

Dave
 
J

Joost Diepenmaat

Thanks for the replies. Unfortunately in both of those instances, I
will still have to escape the xhtml portion. That is the part I am
looking at not having to do. The <pre> tags work for preformatted
text and wouldn't apply to changing or writing content in javascript.
The second example, I will still have to escape the xhtml code because
it falls between two quotes (""). Thanks again though. If anyone
else has any ideas, please let me know.

As I said above:

That *will* escape anything.
 
H

hendedav

As I said above:


That *will* escape anything.


Joost,
I tried, as you suggested, and it didn't work as I thought. The
below is the code:

var element = document.getElementById("divTFooter");
element.appendChild(document.createTextNode("this is an example of a
link: <a href="test.html">link</a>"));

and I got a "missing ) after argument list" error after the quote
right before the test.html name. I will need to find something that
doesn't have quotes as the delimiters. Thanks though, any other
ideas?

Dave
 
J

Joost Diepenmaat

Joost,
I tried, as you suggested, and it didn't work as I thought. The
below is the code:

var element = document.getElementById("divTFooter");
element.appendChild(document.createTextNode("this is an example of a
link: <a href="test.html">link</a>"));

and I got a "missing ) after argument list" error after the quote
right before the test.html name. I will need to find something that
doesn't have quotes as the delimiters. Thanks though, any other
ideas?

Oh right. You mean you have to escape the quotes inside a quoted
string. There's no way around that if you use string literals.

It's annoying, of course, but I don't really see that there is a
problem. You generally don't put large sections of text in string
literals, and if you really want to you can easily write a short script
that generates the string literal for you:

$ perl -e '$_=join("",<>);s/\n/\\n/g;s/"/\\"/g;print qq("$_")' my.html
 
H

hendedav

Oh right. You mean you have to escape the quotes inside a quoted
string. There's no way around that if you use string literals.

It's annoying, of course, but I don't really see that there is a
problem. You generally don't put large sections of text in string
literals, and if you really want to you can easily write a short script
that generates the string literal for you:

$ perl -e '$_=join("",<>);s/\n/\\n/g;s/"/\\"/g;print qq("$_")' my.html


Can you think of any other way to accomplish what I am doing via
javascript? Thanks for the perl snippet btw.

Dave
 
J

Joost Diepenmaat

Can you think of any other way to accomplish what I am doing via
javascript? Thanks for the perl snippet btw.

Load the html snippet via XMLHttpRequest, or put it in the DOM
somewhere. If you want to put it in the JS file, you don't have any
option but to use property quoted string literals.
 
J

Jim

Does this not work?

var element = document.getElementById("divTFooter");
element.appendChild(document.createTextNode('this is an example of a
link: <a href="test.html">link</a>'));

note the use of SINGLE quotes (')

Jim
 
S

SAM

(e-mail address removed) a écrit :
I tried, as you suggested, and it didn't work as I thought. The
below is the code:

var element = document.getElementById("divTFooter");
element.appendChild(document.createTextNode("this is an example of a
link: <a href="test.html">link</a>"));

and I got a "missing ) after argument list" error after the quote
right before the test.html name.

With the string delimited by simple quotes instead of double ones :

element.appendChild(document.createTextNode('this is an example of a
link: <a href="test.html">link<\/a>'));


Or escaping the inside double quotes :

element.appendChild(document.createTextNode("this is an example of a
link: <a href=\"test.html\">link<\/a>"));
 
H

hendedav

Does this not work?

var element = document.getElementById("divTFooter");
element.appendChild(document.createTextNode('this is an example of a
link: <a href="test.html">link</a>'));

note the use of SINGLE quotes (')

Jim

Jim, it would work except if there is javascript in any of the xhtml,
then I am back to square one. I thought I had come across a way to do
it once, and apparently didn't write it down or store an example. The
reason I am trying to do this is because I have several pages that
have the same text/code and I would like to have javascript populate
those areas so a single change to the javascript will be a global
change. Using several document.writes gets messy so I was looking for
a better way.

Dave
 
J

Joost Diepenmaat

Jim, it would work except if there is javascript in any of the xhtml,
then I am back to square one.

How can something like that suddenly appear in the javascript source?
If you are generating the script, you can also automatically escape the
string literals. If you are hand-writing the script, you can *see* what
you need to escape (and there are tools to help you with it).

If the strings are in fact read from somewhere else (i.e. they aren't
string literals) you don't need to quote them (or at least, you should
use the correct formatting for whatever source you're using.
Using several document.writes gets messy so I was looking for
a better way.

document.write is messy anyway. Seems to me you're actually looking for
some kind of include mechanism for HTML. There are plenty of server-side
solutions that do that way easier and more reliable than
document.write() in client-side javascript.
 
H

hendedav

How can something like that suddenly appear in the javascript source?
If you are generating the script, you can also automatically escape the
string literals. If you are hand-writing the script, you can *see* what
you need to escape (and there are tools to help you with it).

If the strings are in fact read from somewhere else (i.e. they aren't
string literals) you don't need to quote them (or at least, you should
use the correct formatting for whatever source you're using.


document.write is messy anyway. Seems to me you're actually looking for
some kind of include mechanism for HTML. There are plenty of server-side
solutions that do that way easier and more reliable than
document.write() in client-side javascript.


The code won't suddenly appear. I am typing it out, but it is a pain
to have to escape the quotes (especially when taking from code that is
existing). I was looking for something like how perl allows a
programmer to write a block of text without having to escape
anything. I also wouldn't want to have this one the server side
because it is just adding overhead to the server that doesn't need to
be. I guess javascript doesn't have anything like that, which sucks.

Dave
 
J

Joost Diepenmaat

The code won't suddenly appear. I am typing it out, but it is a pain
to have to escape the quotes (especially when taking from code that is
existing).

Ok, well I can understand that it's a pain.
I was looking for something like how perl allows a programmer to
write a block of text without having to escape anything.

Doesn't exist in javascript. JS doesn't even have multi-line string
literals.
I also wouldn't want to have this one the server side because it is
just adding overhead to the server that doesn't need to be. I guess
javascript doesn't have anything like that, which sucks.

Loading multiple files isn't necessarily better than pasting those files
together before serving them (using SSI, for instance), though it may
reduce traffic if the client caches the reused parts.

Personally, what I do for simple sites is generate static HTML from
templates off-line and then upload the generated HTML. For large sites
you may need something a bit more complex, but it works fine if you have
less than a couple of hundreds of pages of mostly static pages, and it's
*way* easier to maintain - for one thing, you only have to have a single
instance of each re-used block of HTML (since you can go all out on
templating, instead of only using it for the big pieces), and also, with
a little care you can generate all your navigation / menus
automatically.

I use perl to do that, by the way: either with Template-Toolkit (see the
ttree command) or my own Text::pSP module.
 
T

Thomas 'PointedEars' Lahn

Joost said:
Doesn't exist in javascript. JS doesn't even have multi-line string
literals.

As a matter of fact, it has. But the trailing \ in a string literal is a
proprietary extension of the Specification.

Specified E4X literals come closer to what one could consider multi-line
strings, especially when in comes to markup:

var foo = <bar>
<baz/>
</bar>;

The XML value that `foo' holds then may then be converted to string:

var bar = foo.toXMLString();

But E4X literals require JavaScript 1.6 (Firefox 1.5+) or another
implementation that supports ECMA-357 (which AFAIK does not include JScript).


PointedEars
 

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
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top