<pre> with js

J

Jan C. Faerber

I read that in XML you can use CDATA sections to show code in a
browser.
When you use HTML you can use the <pre> tag.
It works fine. I think <code> makes just the font style.

But when I use <script language="[...] in the <pre> section
and when I don't change <table> into &lt;table&gt; and so on
(well, I forgot at the moment what exactly prevents the browser
to use the code as usual) the <pre> tag becomes useless.

http://www.lovedieu.eu/javascript/resize.html
Here I tried to present some code including
<script etc. > and { ... } sections.
It was not working.

Is there a stronger tag than the <pre> tag to present code?
 
J

Jukka K. Korpela

Jan said:
I read that in XML you can use CDATA sections to show code in a
browser.

You misread, more or less. CDATA sections are for inserting character data
(C = character) to be treated as such, even if it contains constructs that
would normally be taken as tags or entity or character references in XML. It
has nothing particular to do with "code", whatever that means, though of
course computer code often contains "<" or "&".

XML as such is virtually useless on the Web.
When you use HTML you can use the <pre> tag.

That's preformatted text. It does not have anything particular to do with
"code" or with CDATA. It just means that whitespace is preserved.
It works fine.

For some values of "work" and "fine", yes.
I think <code> makes just the font style.

The <code> markup indicates that the content is computer code. It does not
override normal HTML or XML rules for "<" or "&" in any way. On graphic
browsers, the content is typically rendered in a monospace font by default,
but that's coincidental.
But when I use <script language="[...] in the <pre> section
and when I don't change <table> into &lt;table&gt; and so on
(well, I forgot at the moment what exactly prevents the browser
to use the code as usual) the <pre> tag becomes useless.

The description is confused but sufficient for indicating that you expected
Is there a stronger tag than the <pre> tag to present code?

The robust approach is to "escape" the characters "&" and "<" as required by
HTML rules. It's easy, and if you need to do it often, you can surely create
a macro or a program for the purpose, if you are qualified enough to give
advice to others in matters of programming.

But since you asked, there's a "stronger" tag, still supported by browsers,
<xmp>, though it has been deprecated since its origin, through millennia.,
and isn't even part of any comtemporary HTML specification.
 
J

Jan C. Faerber

The robust approach is to "escape" the characters "&" and "<" as required by
HTML rules. It's easy, and if you need to do it often, you can surely create
a macro or a program for the purpose, if you are qualified enough to give
advice to others in matters of programming.

After some copy&fix&paste I got a form which can do it to get a nice
result:
http://www.lovedieu.eu/alt.html/fred.html and I updated my link
http://www.lovedieu.eu/alt.html/resize2.html

Thank you for your hints - only I still don't understand the problem
with "&".
Is it better to change them to "&amp;"?
 
D

dorayme

Is there a stronger tag than the <pre> tag to present code?

The robust approach is to "escape" the characters "&" and "<" as required by
HTML rules. It's easy, and if you need to do it often, you can surely create
a macro [/QUOTE]

Somewhere along the line, to get "<" and ">", I got or made:

Replace

<([^<>]+)>

with

\&lt;\1\&gt;

in my patterns in BBEdit.
 
J

Jan C. Faerber

But when I use <script language="[...]   in the <pre> section
and when I don't change <table> into &lt;table&gt; and so on
(well, I forgot at the moment what exactly prevents the browser
to use the code as usual) the <pre> tag becomes useless.

The description is confused but sufficient for indicating that you expected
<pre> to have a meaning different from its defined meaning. It is indeed
useless or worse then.


hm... I think they aimed to achieve that with some good reason.
I found that the innerHTML e.g. of a <div> will also not allow to past
the html declaration or javascript syntax completly. But this is js
topic.
Same with ajax.
Just wanted to add that the exact point of time might be relevant when
some code is used by a browser.
So maybe a script should work within a <pre> section?
But that is really js topic. Sorry. Or?
At least it gets truncated for the presentation.
I did not continue any further studies in that direction.
Securityreasons? Seperate Programming from Mark-Up? etc...
 
A

Adrienne Boswell

only I still don't understand the problem
with "&".
Is it better to change them to "&amp;"?

Yes, always escape ampersand.

In my authoring, I always use the whole word "and" unless the ampersand is
a part of a company's name, like Ben & Jerry's.
 
A

Andy Dingley

XML as such is virtually useless on the Web.

If we accept "The Web" as "that part of the internet that's
addressable by URLs", then XML is widely used and useful within it.
The web is accessed by user agents, not all of which are either
interactive, nor "web browsers". Many of these user agents do indeed
make good use of XML.
 
J

Jukka K. Korpela

Andy said:
If we accept "The Web" as "that part of the internet that's
addressable by URLs", then XML is widely used and useful within it.

Indeed, but not as such. It is used as a data format behind the scenes, not
as delivery format to be directly displayed by browsers.

Of course I could and should have formulated my statement more carefully.
The point is that XML is not a replacement for HTML.
 
J

Jan C. Faerber

Hey - hey - hey!
Don't spoil my quoted ampersand!! I wrote "&amp;"!
Yes, always escape ampersand.  

I wonder if you never get a mess with a recursive problem
like ( ( ( &amp; ) amp; ) amp; ) amp;
In my authoring, I always use the whole word "and" unless the ampersand is
a part of a company's name, like Ben & Jerry's.

which authoring?
 
J

John Hosking

Hey - hey - hey!
Don't spoil my quoted ampersand!! I wrote "&amp;"!

She didn't spoil anything; your text was intact in her reply when I viewed
it in my newsreader. It's probably only messed up in your reply because
you're using GoogleGroups' Web interface to usenet instead of an actual
newsreader.
I wonder if you never get a mess with a recursive problem
like ( ( ( &amp; ) amp; ) amp; ) amp;

Yes, it's possible. Like maybe if you use GoogleGroups. Whether Adrienne
herself ever has this problem, I don't know; she seems to be a competent
coder.
which authoring?

HTML authoring, of course, as for the World Wide Web.
 
A

Adrienne Boswell

Hey - hey - hey!
Don't spoil my quoted ampersand!! I wrote "&amp;"!

It showed up for me as a single ampersand character between two
quotation marks.
I wonder if you never get a mess with a recursive problem
like ( ( ( &amp; ) amp; ) amp; ) amp;

Just so, we see an example of that here. You wrote &amp; but I only saw
& - had you escaped the first ampersand (&amp;mp;) I _might_ have seen
it that way (no guarantee because you're using Google Groups).
which authoring?

HTML authoring.
 
J

Jan C. Faerber

It showed up for me as a single ampersand character between two
quotation marks.  


Just so, we see an example of that here.  You wrote & but I only saw
& - had you escaped the first ampersand (&mp;) I _might_ have seen
it that way (no guarantee because you're using Google Groups).

Oh - sorry!
Good to know that thing.
I should use the backslash.
\\&\\a\\m\\p\\;
HTML authoring.  

For a magazine? Or for the web? Or for books?
No matter - go on.
 

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,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top