PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.www.webmaster > js write a <hr>
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
js write a <hr>

Réponse
 
LinkBack Outils de la discussion
Vieux 11/09/2007, 05h52   #1
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut js write a <hr>

Can't for the life of me figure out how to get a hr into a js write.

http://blinkynet.net/humor/index.html

I'd like a *scripted* <hr> just before the first line created by the
fist little script do that it separates the quote of the day from the
page intro above it. The reason I want it to be part of the script is
so if scripts are disabled and the separator isn't needed, it's not
rendered. (The nonscripted <hr> *after* the quote of the day will be
all the separation I need between the intro and the list.)

I've been able to make this work just fine with "<hr />" being the first
element in the first document.write:

document.write("<hr />" + "foo")

But validation fails with a you-can't-use-a-<hr>-there error.

So I look at W3Schools, which I think I've seen recommended here for
tutorials -- I'm obviously new to js.

Barely into the js tute, I see:

<q>

A JavaScript statement like this: document.write("<h1>" + name +
"</h1>") can write a variable text into an HTML page

</q>

Now if they can get a <h1> in there, why can't I get a <hr> in, using the
same method? Yes, I tried it with and without closing tags. Validator
won't have anything do do with it.

Is there a *simple* way to do that I want? No, I'm not going to do
twelve lines of code for one hr.


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 05h55   #2
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:
> Can't for the life of me figure out how to get a hr into a js write.
>
> http://blinkynet.net/humor/index.html
>
> I'd like a *scripted* <hr> just before the first line created by the
> fist little script do that it separates the quote of the day from the


FIRST little script SO that it separates... Grrrrrr.

> page intro above it. The reason I want it to be part of the script is
> so if scripts are disabled and the separator isn't needed, it's not
> rendered. (The nonscripted <hr> *after* the quote of the day will be
> all the separation I need between the intro and the list.)
>
> I've been able to make this work just fine with "<hr />" being the first
> element in the first document.write:
>
> document.write("<hr />" + "foo")
>
> But validation fails with a you-can't-use-a-<hr>-there error.
>
> So I look at W3Schools, which I think I've seen recommended here for
> tutorials -- I'm obviously new to js.
>
> Barely into the js tute, I see:
>
><q>
>
> A JavaScript statement like this: document.write("<h1>" + name +
> "</h1>") can write a variable text into an HTML page
>
></q>
>
> Now if they can get a <h1> in there, why can't I get a <hr> in, using the
> same method? Yes, I tried it with and without closing tags. Validator
> won't have anything do do with it.
>
> Is there a *simple* way to do that I want? No, I'm not going to do
> twelve lines of code for one hr.
>
>



--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 06h12   #3
Steve Sobol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

On 2007-09-11, Blinky the Shark <no.spam@box.invalid> wrote:
> Can't for the life of me figure out how to get a hr into a js write.
>
> http://blinkynet.net/humor/index.html
>
> I'd like a *scripted* <hr> just before the first line created by the


document.write() won't work?

> But validation fails with a you-can't-use-a-<hr>-there error.


So this would be an HTML issue, not a js issue. To which standard are you
writing? HTML 4 Transitional? Strict? XHTML Transitional? Strict?


--
Steve Sobol, Victorville, California PGP:0xE3AE35ED
"Drench yourself in words unspoken / Live your life with arms wide open
Today is where your book begins / The rest is still unwritten"
- Natasha Beddingfield

  Réponse avec citation
Vieux 11/09/2007, 09h46   #4
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Steve Sobol wrote:
> On 2007-09-11, Blinky the Shark <no.spam@box.invalid> wrote:
>> Can't for the life of me figure out how to get a hr into a js write.
>>
>> http://blinkynet.net/humor/index.html
>>
>> I'd like a *scripted* <hr> just before the first line created by the

>
> document.write() won't work?


As I said, it works, but it won't validate.

>> But validation fails with a you-can't-use-a-<hr>-there error.

>
> So this would be an HTML issue, not a js issue. To which standard are you


Call it what you'd like. As I stated, the problem is that I can't - via
js - figure out how to get something to validate that validates when I
do the same thing without the script. Putting an <hr> in the body
(correct - not within a <p> or <div> validates, but it won't if js
places it there.

> writing? HTML 4 Transitional? Strict? XHTML Transitional? Strict?


That's in my doctype. That kind of thing is why I gave the link.

I've been bad: XHTML because when I did a total rewrite of the site a
couple hears ago I thought that was The Coming Thing and wanted to not
go backward; but served as HTML because then I learned that browser
support was supposedly iffy for XHTML.


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 10h11   #5
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:
> Steve Sobol wrote:
>> On 2007-09-11, Blinky the Shark <no.spam@box.invalid> wrote:
>>> Can't for the life of me figure out how to get a hr into a js write.
>>>
>>> http://blinkynet.net/humor/index.html
>>>
>>> I'd like a *scripted* <hr> just before the first line created by the

>>
>> document.write() won't work?

>
> As I said, it works, but it won't validate.
>
>>> But validation fails with a you-can't-use-a-<hr>-there error.

>>
>> So this would be an HTML issue, not a js issue. To which standard are you

>
> Call it what you'd like. As I stated, the problem is that I can't - via
> js - figure out how to get something to validate that validates when I
> do the same thing without the script. Putting an <hr> in the body
> (correct - not within a <p> or <div> validates, but it won't if js
> places it there.
>
>> writing? HTML 4 Transitional? Strict? XHTML Transitional? Strict?

>
> That's in my doctype. That kind of thing is why I gave the link.
>
> I've been bad: XHTML because when I did a total rewrite of the site a
> couple hears ago I thought that was The Coming Thing and wanted to not
> go backward; but served as HTML because then I learned that browser
> support was supposedly iffy for XHTML.


Here's test copy of that page with what I'm *trying* to do. See commented div.
When that <hr> is generated by a js document.write, it won't validate.
If I just put a <hr> in the same place in the page (in that div, or
just in the body without that div existing, the <hr> page validates.

http://blinkynet.net/humor/index2.html

Sorry I didn't link *this* before.

Again, I want the <hr> to be js-generated so that it's not there when js
is disabled -- in that case it's not needed because the whole
quote-of-the-day section isn't rendered.


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 10h35   #6
Dylan Parry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:

> document.write("<hr />" + "foo")
>
> But validation fails with a you-can't-use-a-<hr>-there error.


Try:

document.write("<hr \/>" + "foo");

When using document.write() combined with XHTML, you need to escape the
closing slashes in end tags otherwise it won't validate. It should still
/work/ okay without it though.

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
  Réponse avec citation
Vieux 11/09/2007, 11h13   #7
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Dylan Parry wrote:
> Blinky the Shark wrote:
>
>> document.write("<hr />" + "foo")
>>
>> But validation fails with a you-can't-use-a-<hr>-there error.

>
> Try:
>
> document.write("<hr \/>" + "foo");
>
> When using document.write() combined with XHTML, you need to escape the
> closing slashes in end tags otherwise it won't validate. It should still
> /work/ okay without it though.


Nope. As before: works but won't validate.

Here, with your idea: http://blinkynet.net/humor/index2.html

Try it.

--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 12h00   #8
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:
> Can't for the life of me figure out how to get a hr into a js write.
>
> http://blinkynet.net/humor/index.html
>
> I'd like a *scripted* <hr> just before the first line created by the
> fist little script do that it separates the quote of the day from the
> page intro above it. The reason I want it to be part of the script is
> so if scripts are disabled and the separator isn't needed, it's not
> rendered. (The nonscripted <hr> *after* the quote of the day will be
> all the separation I need between the intro and the list.)
>
> I've been able to make this work just fine with "<hr />" being the first
> element in the first document.write:
>
> document.write("<hr />" + "foo")
>
> But validation fails with a you-can't-use-a-<hr>-there error.
>
> So I look at W3Schools, which I think I've seen recommended here for
> tutorials -- I'm obviously new to js.
>
> Barely into the js tute, I see:
>
> <q>
>
> A JavaScript statement like this: document.write("<h1>" + name +
> "</h1>") can write a variable text into an HTML page
>
> </q>
>
> Now if they can get a <h1> in there, why can't I get a <hr> in, using the
> same method? Yes, I tried it with and without closing tags. Validator
> won't have anything do do with it.
>
> Is there a *simple* way to do that I want? No, I'm not going to do
> twelve lines of code for one hr.
>
>


Are you sure it isn't just the validator not understanding js? Most
don't, and try to parse the js as html somehow. It doesn't mean the
page is invalid - just that the validator doesn't understand it.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Réponse avec citation
Vieux 11/09/2007, 12h19   #9
Dylan Parry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:

>> When using document.write() combined with XHTML, you need to escape the
>> closing slashes in end tags otherwise it won't validate. It should still
>> /work/ okay without it though.

>
> Nope. As before: works but won't validate.


Now that is odd. I've had this error pop up time and again when I've
forgotten to escape closing slashes, but adding in the escape has always
fixed the problem. :s

> Here, with your idea: http://blinkynet.net/humor/index2.html


Out of interest, does the same error appear when you substitute the
<hr/> element with another empty element such as <img/>?

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
  Réponse avec citation
Vieux 11/09/2007, 15h29   #10
GreyWyvern
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

And lo, Blinky the Shark didst speak in alt.www.webmaster:

> Can't for the life of me figure out how to get a hr into a js write.
>
> http://blinkynet.net/humor/index.html
>
> I'd like a *scripted* <hr> just before the first line created by the
> fist little script do that it separates the quote of the day from the
> page intro above it. The reason I want it to be part of the script is
> so if scripts are disabled and the separator isn't needed, it's not
> rendered. (The nonscripted <hr> *after* the quote of the day will be
> all the separation I need between the intro and the list.)
>
> I've been able to make this work just fine with "<hr />" being the first
> element in the first document.write:
>
> document.write("<hr />" + "foo")
>
> But validation fails with a you-can't-use-a-<hr>-there error.


/me boggles at all the other responses in this thread.

XHTML is not just HTML with a few fancy markup changes. The way the
markup is *interpreted* changes as well. XHTML is meant to be valid XML
and thus must follow all the rules as XML.

One of the rules is that you can't just include text containing angle
brackets or ampersands without them being interpreted as markup and
entities. Unless you comment it out, or strictly define it as CDATA
(character data), the XML parser will assume it is PCDATA (parsed
character data), or IOW: markup.

The element which which you're having problems is this one:


<script type="text/javascript">
document.write("<hr \/>" + "Today's alleged Yogi-ism:")
</script>


If this were normal HTML, the parser would recognise the script element as
special and switch to the JS parser for all the contents. The XML parser
doesn't do this. In XHTML, the script element is *not* special and
elements found inside it are interpreted as actual elements, whether they
are inside document.write's or not.

So, in order to get it to validate, you need to tell the parser that
what's inside the script element is special. You can do this in one of
two ways, but there are caveats about both.

1) Simply use SGML comments to hide the code:


<script type="text/javascript"><!--
document.write("<hr \/>" + "Today's alleged Yogi-ism:")
--></script>


There are two issues with this though. The first is, though browsers you
view this in will interpret this bit of code the way you think it should
work, that's only because you are serving XHTML code to the HTML tag-soup
parser by using the text/html MIME-type. *Technically* the XML parser is
allowed to ignore *anything* within SGML comments, *even code*. So
interpreted as real XML, the above snippet is entirely equivalent to:


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


The second issue is that the double hyphen is a special sequence within
SGML comments which tell it the comment is ending. Unfortunately, the
double hyphen is also used in Javascript as the decrement operator (x--)..
So commenting out your code this way precludes you from using the
decrement operator at all or else some of your code may appear as text,
and/or your document will not validate. This second issue affects plain
HTML as well.

2) Use the XML <![CDATA[ ... ]]> structure to mark the boundaries of your
code.


<script type="text/javascript"><![CDATA[
document.write("<hr \/>" + "Today's alleged Yogi-ism:")
]]></script>


This is the preferred way to do things in XML, but it won't work in your
document. Why not? Because you are serving it with a text/html
MIME-type. This means that markup which *should* be parsed with the XML
parser is being parsed with the HTML tag-soup parser instead. This is
precisely why, in your existing document, the JS still works, even though
the document doesn't validate as XHTML.

In order for the above to function properly in both an HTML and XHTML
context, you'd need to use a comment system like so[1]:


<script type="text/javascript"><!--//--><![CDATA[//><!--
...
//--><!]]></script>


That's pretty crazy, eh?

So, to summarize: Your markup is being delivered to the browser using the
text/html MIME-type. This allows it to render as you think it should
because the HTML tag-soup parser is being used. However, the W3C
validator is interpreting your XHTML markup as if it were served with the
correct MIME-type: application/xhtml+xml OR text/xml

This is leading to validation errors which are only errors of context.
The code is being rendered by browsers in one context, while the validator
is viewing it in another. I would recommend that if you don't yet fully
understand the difference between these two contexts, you should return to
plain HTML4, since that is exactly how you are telling browsers to
interpret your XHTML markup anyway.


One last thing to note, in XHTML sent with one of the proper MIME-type's
above, certain JS methods which have the potential to allow easy breakage
of the strict XML rules have been disabled. This includes
document.write() and also innerHTML() and its relatives. So the code you
are trying to use, even if you did try switching to the correct
MIME-types, would not even work, instead throwing JS errors.

An equivalent would be (commented to work in both text/html and text/xml):


<script type="text/javascript" id="replaceMe"><!--//--><![CDATA[//><!--
var hr = document.createElement('hr');
var thisElem = document.getElementById('replaceMe');
var parentElem = thisElem.parentNode;
parentElem.replaceChild(hr, thisElem);
parentElem.appendChild(document.createTextNode("To day's alleged
Yogi-ism:"));
//--><!]]></script>


Or the somewhat simpler to understand, but won't work in IE 5.5 and
earlier[2]:


<script type="text/javascript" id="replaceMe"><!--//--><![CDATA[//><!--
var frag = document.createDocumentFragment();
var hr = document.createElement('hr');
frag.appendChild(hr);
frag.appendChild(document.createTextNode("Today's alleged
Yogi-ism:"));
var thisElem = document.getElementById('replaceMe');
thisElem.parentNode.replaceChild(frag, thisElem);
//--><!]]></script>


More complicated, yesh. However the purpose is to enforce, as much as
possible, dynamically added code to be valid XML in itself.

If this is all too much for you to handle, then you should seriously
consider a step back from XHTML and stick with HTML. Like I said, it's
more than just a few extra rules tacked onto HTML 4.01; XHTML is an
entirely different way to parse documents.

HTML is entirely sufficient for most web documents of any kind.

> So I look at W3Schools, which I think I've seen recommended here for
> tutorials -- I'm obviously new to js.
>
> Barely into the js tute, I see:
>
> <q>
>
> A JavaScript statement like this: document.write("<h1>" + name +
> "</h1>") can write a variable text into an HTML page
>
> </q>
>
> Now if they can get a <h1> in there, why can't I get a <hr> in, using the
> same method? Yes, I tried it with and without closing tags. Validator
> won't have anything do do with it.


It is simply because you are using an XHTML DOCTYPE. The validator is
expecting you to follow the rules as if you were using one of the correct
MIME-types.

> Is there a *simple* way to do that I want? No, I'm not going to do
> twelve lines of code for one hr.


Yesh, change the DOCTYPE back to HTML 4.01. Simple.

Grey

[1] http://www.hixie.ch/advocacy/xhtml

[2] IE 5.5 and earlier do not support the DocumentFragment object.

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
  Réponse avec citation
Vieux 11/09/2007, 16h44   #11
GreyWyvern
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

And lo, GreyWyvern didst speak in alt.www.webmaster:

> So, in order to get it to validate, you need to tell the parser that
> what's inside the script element is special. You can do this in one of
> two ways, but there are caveats about both.
>
> 1) Simply use SGML comments to hide the code:


[snip]

> 2) Use the XML <![CDATA[ ... ]]> structure to mark the boundaries of
> your code.


[snip]

There is, of course, a third option which I have forgotten, and that is to
use an external javascript file.


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

In file: writehr.js
document.write("<hr />" + "Today's alleged Yogi-ism:");


Although the same caveat about document.write() not working when using the
text/xml or application/xhtml+xml MIME-types still applies.

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
  Réponse avec citation
Vieux 11/09/2007, 17h10   #12
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Dylan Parry wrote:
> Blinky the Shark wrote:
>
>>> When using document.write() combined with XHTML, you need to escape the
>>> closing slashes in end tags otherwise it won't validate. It should still
>>> /work/ okay without it though.

>>
>> Nope. As before: works but won't validate.

>
> Now that is odd. I've had this error pop up time and again when I've
> forgotten to escape closing slashes, but adding in the escape has always
> fixed the problem. :s
>
>> Here, with your idea: http://blinkynet.net/humor/index2.html

>
> Out of interest, does the same error appear when you substitute the
><hr/> element with another empty element such as <img/>?


Yes. In one incarnation of that same part of the page, I was trying to
use a <br> in that document.write, and I got the same error(s).


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 17h16   #13
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

GreyWyvern wrote:
> And lo, Blinky the Shark didst speak in alt.www.webmaster:
>
>> Can't for the life of me figure out how to get a hr into a js write.
>>
>> http://blinkynet.net/humor/index.html
>>
>> I'd like a *scripted* <hr> just before the first line created by the
>> fist little script do that it separates the quote of the day from the
>> page intro above it. The reason I want it to be part of the script is
>> so if scripts are disabled and the separator isn't needed, it's not
>> rendered. (The nonscripted <hr> *after* the quote of the day will be
>> all the separation I need between the intro and the list.)
>>
>> I've been able to make this work just fine with "<hr />" being the first
>> element in the first document.write:
>>
>> document.write("<hr />" + "foo")
>>
>> But validation fails with a you-can't-use-a-<hr>-there error.

>
> /me boggles at all the other responses in this thread.
>
> XHTML is not just HTML with a few fancy markup changes. The way the
> markup is *interpreted* changes as well. XHTML is meant to be valid XML
> and thus must follow all the rules as XML.
>
> One of the rules is that you can't just include text containing angle
> brackets or ampersands without them being interpreted as markup and
> entities. Unless you comment it out, or strictly define it as CDATA
> (character data), the XML parser will assume it is PCDATA (parsed
> character data), or IOW: markup.
>
> The element which which you're having problems is this one:
>
>
><script type="text/javascript">
> document.write("<hr \/>" + "Today's alleged Yogi-ism:")
></script>
>
>
> If this were normal HTML, the parser would recognise the script element as
> special and switch to the JS parser for all the contents. The XML parser
> doesn't do this. In XHTML, the script element is *not* special and
> elements found inside it are interpreted as actual elements, whether they
> are inside document.write's or not.
>
> So, in order to get it to validate, you need to tell the parser that
> what's inside the script element is special. You can do this in one of
> two ways, but there are caveats about both.
>
> 1) Simply use SGML comments to hide the code:
>
>
><script type="text/javascript"><!--
> document.write("<hr \/>" + "Today's alleged Yogi-ism:")
> --></script>
>
>
> There are two issues with this though. The first is, though browsers you
> view this in will interpret this bit of code the way you think it should
> work, that's only because you are serving XHTML code to the HTML tag-soup
> parser by using the text/html MIME-type. *Technically* the XML parser is
> allowed to ignore *anything* within SGML comments, *even code*. So
> interpreted as real XML, the above snippet is entirely equivalent to:
>
>
><script type="text/javascript"></script>
>
>
> The second issue is that the double hyphen is a special sequence within
> SGML comments which tell it the comment is ending. Unfortunately, the
> double hyphen is also used in Javascript as the decrement operator (x--).
> So commenting out your code this way precludes you from using the
> decrement operator at all or else some of your code may appear as text,
> and/or your document will not validate. This second issue affects plain
> HTML as well.
>
> 2) Use the XML <![CDATA[ ... ]]> structure to mark the boundaries of your
> code.
>
>
><script type="text/javascript"><![CDATA[
> document.write("<hr \/>" + "Today's alleged Yogi-ism:")
> ]]></script>
>
>
> This is the preferred way to do things in XML, but it won't work in your
> document. Why not? Because you are serving it with a text/html
> MIME-type. This means that markup which *should* be parsed with the XML
> parser is being parsed with the HTML tag-soup parser instead. This is
> precisely why, in your existing document, the JS still works, even though
> the document doesn't validate as XHTML.
>
> In order for the above to function properly in both an HTML and XHTML
> context, you'd need to use a comment system like so[1]:
>
>
><script type="text/javascript"><!--//--><![CDATA[//><!--
> ...
> //--><!]]></script>
>
>
> That's pretty crazy, eh?
>
> So, to summarize: Your markup is being delivered to the browser using the
> text/html MIME-type. This allows it to render as you think it should
> because the HTML tag-soup parser is being used. However, the W3C
> validator is interpreting your XHTML markup as if it were served with the
> correct MIME-type: application/xhtml+xml OR text/xml
>
> This is leading to validation errors which are only errors of context.
> The code is being rendered by browsers in one context, while the validator
> is viewing it in another. I would recommend that if you don't yet fully
> understand the difference between these two contexts, you should return to
> plain HTML4, since that is exactly how you are telling browsers to
> interpret your XHTML markup anyway.
>
>
> One last thing to note, in XHTML sent with one of the proper MIME-type's
> above, certain JS methods which have the potential to allow easy breakage
> of the strict XML rules have been disabled. This includes
> document.write() and also innerHTML() and its relatives. So the code you
> are trying to use, even if you did try switching to the correct
> MIME-types, would not even work, instead throwing JS errors.
>
> An equivalent would be (commented to work in both text/html and text/xml):
>
>
><script type="text/javascript" id="replaceMe"><!--//--><![CDATA[//><!--
> var hr = document.createElement('hr');
> var thisElem = document.getElementById('replaceMe');
> var parentElem = thisElem.parentNode;
> parentElem.replaceChild(hr, thisElem);
> parentElem.appendChild(document.createTextNode("To day's alleged
> Yogi-ism:"));
> //--><!]]></script>
>
>
> Or the somewhat simpler to understand, but won't work in IE 5.5 and
> earlier[2]:
>
>
><script type="text/javascript" id="replaceMe"><!--//--><![CDATA[//><!--
> var frag = document.createDocumentFragment();
> var hr = document.createElement('hr');
> frag.appendChild(hr);
> frag.appendChild(document.createTextNode("Today's alleged
> Yogi-ism:"));
> var thisElem = document.getElementById('replaceMe');
> thisElem.parentNode.replaceChild(frag, thisElem);
> //--><!]]></script>
>
>
> More complicated, yesh. However the purpose is to enforce, as much as
> possible, dynamically added code to be valid XML in itself.
>
> If this is all too much for you to handle, then you should seriously
> consider a step back from XHTML and stick with HTML. Like I said, it's
> more than just a few extra rules tacked onto HTML 4.01; XHTML is an
> entirely different way to parse documents.
>
> HTML is entirely sufficient for most web documents of any kind.
>
>> So I look at W3Schools, which I think I've seen recommended here for
>> tutorials -- I'm obviously new to js.
>>
>> Barely into the js tute, I see:
>>
>> <q>
>>
>> A JavaScript statement like this: document.write("<h1>" + name +
>> "</h1>") can write a variable text into an HTML page
>>
>> </q>
>>
>> Now if they can get a <h1> in there, why can't I get a <hr> in, using the
>> same method? Yes, I tried it with and without closing tags. Validator
>> won't have anything do do with it.

>
> It is simply because you are using an XHTML DOCTYPE. The validator is
> expecting you to follow the rules as if you were using one of the correct
> MIME-types.
>
>> Is there a *simple* way to do that I want? No, I'm not going to do
>> twelve lines of code for one hr.

>
> Yesh, change the DOCTYPE back to HTML 4.01. Simple.
>
> Grey
>
> [1] http://www.hixie.ch/advocacy/xhtml
>
> [2] IE 5.5 and earlier do not support the DocumentFragment object.


Thanks. Based on your input, it looks like I will remove the <hr> and
not rewrite the entire site.

--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 17h21   #14
GreyWyvern
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

And lo, Blinky the Shark didst speak in alt.www.webmaster:

> Thanks. Based on your input, it looks like I will remove the <hr> and
> not rewrite the entire site.


Glad I could

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
  Réponse avec citation
Vieux 11/09/2007, 17h22   #15
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

GreyWyvern wrote:
> And lo, GreyWyvern didst speak in alt.www.webmaster:
>
>> So, in order to get it to validate, you need to tell the parser that
>> what's inside the script element is special. You can do this in one
>> of two ways, but there are caveats about both.
>>
>> 1) Simply use SGML comments to hide the code:

>
> [snip]
>
>> 2) Use the XML <![CDATA[ ... ]]> structure to mark the boundaries of
>> your code.

>
> [snip]
>
> There is, of course, a third option which I have forgotten, and that
> is to use an external javascript file.
>
>
><script type="text/javascript" src="writehr.js"></script>
>
> In file: writehr.js document.write("<hr />" + "Today's alleged
> Yogi-ism:");
>
>
> Although the same caveat about document.write() not working when using
> the text/xml or application/xhtml+xml MIME-types still applies.


Thanks. I think I will play with this rather than rewriting the whole
site or giving up.


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 17h29   #16
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:
> GreyWyvern wrote:
>> And lo, GreyWyvern didst speak in alt.www.webmaster:
>>
>>> So, in order to get it to validate, you need to tell the parser that
>>> what's inside the script element is special. You can do this in one
>>> of two ways, but there are caveats about both.
>>>
>>> 1) Simply use SGML comments to hide the code:

>>
>> [snip]
>>
>>> 2) Use the XML <![CDATA[ ... ]]> structure to mark the boundaries of
>>> your code.

>>
>> [snip]
>>
>> There is, of course, a third option which I have forgotten, and that
>> is to use an external javascript file.
>>
>>
>><script type="text/javascript" src="writehr.js"></script>
>>
>> In file: writehr.js document.write("<hr />" + "Today's alleged
>> Yogi-ism:");
>>
>>
>> Although the same caveat about document.write() not working when using
>> the text/xml or application/xhtml+xml MIME-types still applies.

>
> Thanks. I think I will play with this rather than rewriting the whole
> site or giving up.


Uh. Wait. So you're saying that this still won't validate unless I
rewrite the site?


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 17h35   #17
GreyWyvern
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

And lo, Blinky the Shark didst speak in alt.www.webmaster:
> Blinky the Shark wrote:
>> GreyWyvern wrote:
>>
>>> There is, of course, a third option which I have forgotten, and that
>>> is to use an external javascript file.
>>>
>>>
>>> <script type="text/javascript" src="writehr.js"></script>
>>>
>>> In file: writehr.js document.write("<hr />" + "Today's alleged
>>> Yogi-ism:");
>>>
>>>
>>> Although the same caveat about document.write() not working when using
>>> the text/xml or application/xhtml+xml MIME-types still applies.

>>
>> Thanks. I think I will play with this rather than rewriting the whole
>> site or giving up.

>
> Uh. Wait. So you're saying that this still won't validate unless I
> rewrite the site?


In a "real" XHTML document, parsed by the XML parser, document.write()
won't work; but parsed as text/html, it will. Moving the statement to an
external file will allow your document to validate mainly because the
validator only considers one file at a time. An external JS file does not
have to follow any XML rules, instead it is only affected by the MIME-type
of the parent document. eg. certain methods are disabled as explained
earlier.

I still think it would be best for you to switch back to HTML 4.01, though
Then you won't have to worry about any of this.

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
  Réponse avec citation
Vieux 11/09/2007, 18h06   #18
Dylan Parry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Blinky the Shark wrote:

> Yes. In one incarnation of that same part of the page, I was trying to
> use a <br> in that document.write, and I got the same error(s).


The answer is in Grey's post. Like a numpty, I'd overlooked the obvious
and not noticed that you hadn't commented out or declared the script
contents as CDATA. The easiest way of solving this though is to move any
scripts to external .js files and reference them as such.

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
  Réponse avec citation
Vieux 11/09/2007, 18h34   #19
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

GreyWyvern wrote:
> And lo, Blinky the Shark didst speak in alt.www.webmaster:
>> Blinky the Shark wrote:
>>> GreyWyvern wrote:
>>>
>>>> There is, of course, a third option which I have forgotten, and that
>>>> is to use an external javascript file.
>>>>
>>>>
>>>> <script type="text/javascript" src="writehr.js"></script>
>>>>
>>>> In file: writehr.js document.write("<hr />" + "Today's alleged
>>>> Yogi-ism:");
>>>>
>>>>
>>>> Although the same caveat about document.write() not working when using
>>>> the text/xml or application/xhtml+xml MIME-types still applies.
>>>
>>> Thanks. I think I will play with this rather than rewriting the whole
>>> site or giving up.

>>
>> Uh. Wait. So you're saying that this still won't validate unless I
>> rewrite the site?

>
> In a "real" XHTML document, parsed by the XML parser, document.write()
> won't work; but parsed as text/html, it will. Moving the statement to an
> external file will allow your document to validate mainly because the
> validator only considers one file at a time. An external JS file does not
> have to follow any XML rules, instead it is only affected by the MIME-type
> of the parent document. eg. certain methods are disabled as explained
> earlier.


Thanks. In the meantime, I did move to an external file and all's well.

Thanks a lot, for a lot of typing.

> I still think it would be best for you to switch back to HTML 4.01, though
> Then you won't have to worry about any of this.


I understand.

--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Vieux 11/09/2007, 18h46   #20
Blinky the Shark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: js write a <hr>

Dylan Parry wrote:
> Blinky the Shark wrote:
>
>> Yes. In one incarnation of that same part of the page, I was trying to
>> use a <br> in that document.write, and I got the same error(s).

>
> The answer is in Grey's post. Like a numpty, I'd overlooked the obvious
> and not noticed that you hadn't commented out or declared the script
> contents as CDATA. The easiest way of solving this though is to move any
> scripts to external .js files and reference them as such.


Thanks. Did. Fine now.


--
Blinky RLU 297263
Killing all posts from Google Groups
The Usenet Improvement Project:
http://improve-usenet.org <----------- New Site Aug 28
  Réponse avec citation
Réponse


Outils de la discussion