quotes in innerHTML

B

Bill Steele

e,g,.
function changeText () {
var content = 'He said "Hello, World!"'
var destination = document.getElementByID("foo")
destination.innerHTML = content
}

<bosdy>

<div id="foo"
<a href="javascript:ChangeText()">What did he say?</a>
The book says the double quotes are fine inside single quotes. In
practice they act as the end of the script. Escaping them doesn't work.
(Escaping single quotes and apostrophes does work. )
 
T

Thomas 'PointedEars' Lahn

Bill said:
e,g,.
function changeText () {
var content = 'He said "Hello, World!"'
var destination = document.getElementByID("foo")
destination.innerHTML = content
}

This script is not going to run in that (X)HTML document; the `script'
element is missing.

That's said:
<div id="foo"

You have forgotten the TAGC delimiter, `>'.

<a href="javascript:ChangeText()">What did he say?</a>

You should not use `javascript:', see the FAQ.

Which book?
says the double quotes are fine inside single quotes.

They are, and vice-versa.
In practice they act as the end of the script.

No, they don't. On top of your syntactically invalid code you have mistyped
`getElementById'. These are case-sensitive programming languages.
Escaping them doesn't work.

Because that is very likely not the cause of your problem.
(Escaping single quotes and apostrophes does work. )

Then perhaps you are using a server-side language to generate that client-
side script, where you use double-quotes to delimit the server-side string
value.

Your question belongs in the message body, not (only) in the Subject header
field value.

Please read and follow the recommendations in <http://jibbering.com/faq/>.


PointedEars
 
A

Andy

Bill Steele said:
e,g,.
function changeText () {
var content = 'He said "Hello, World!"'
var destination = document.getElementByID("foo")
destination.innerHTML = content
}

<bosdy>

<div id="foo"
<a href="javascript:ChangeText()">What did he say?</a>
The book says the double quotes are fine inside single quotes. In
practice they act as the end of the script. Escaping them doesn't work.
(Escaping single quotes and apostrophes does work. )

Have you tried replacing your left double quote with &ldquo; and the right
one with &rdquo; ?

Andy
 
S

SAM

Le 04/08/11 10:02, Andy a écrit :
alert('He said "Hello, World!"'); // He said "Hello, World!"

alert("He said \"Hello, World!\""); // He said "Hello, World!"

alert("'He said \"Hello, World!\" '"); // 'He said "Hello, World!" '

alert('\'He said "Hello, World!" \''); // 'He said "Hello, World!" '

Have you tried replacing your left double quote with &ldquo; and the
right one with &rdquo; ?

Andy

I think, if it isn't too late, that Bill must begin to learn to write.
(and reread himself)

changeText <--> ChangeText
getElementByID ---> getElementById
<bosdy> ---> <body>
 
D

Denis McMahon

Bill Steele wrote:
You should not use `javascript:', see the FAQ.

It's an href, not an event handler, so if it's not the default schema
(http) it needs a schema.

This works in my firefox, didn't test for other browsers:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=us-ascii">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Bill Steele's Broken Code - Fixed</title>
<script type="text/javascript">
function c ()
{
var c, n, t, o;
c = 'He said "Hello, World!"';
n = document.createElement("p");
t = document.createTextNode(c);
n.appendChild(t);
o = document.getElementById("foo").getElementsByTagName("p")[0];
document.getElementById("foo").removeChild(o);
document.getElementById("foo").appendChild(n);
}
</script>
</head>
<body>
<div id="foo">
<p><a href="javascript:c()">What did he say?</a> The book says
the double quotes are fine inside single quotes. In
practice they act as the end of the script. Escaping them
doesn't work. (Escaping single quotes and apostrophes does
work.)</p>
</div>
</body>
</html>

You can see it at http://www.sined.co.uk/tmp/billsteele.htm

It also validates <url:http://validator.w3.org/unicorn/check?
ucn_uri=www.sined.co.uk%2Ftmp%2Fbillsteele.htm&ucn_task=conformance#>

Rgds

Denis McMahon
 
E

Eric Bednarz

Denis McMahon said:
It's an href, not an event handler, so if it's not the default schema
(http) it needs a schema.

This works in my firefox, didn't test for other browsers:

How does that relate to ‘should not’, and did you see the FAQ?

Although allegedly not being the FAQ, but you should read:
<http://www.flightlab.com/~joe/sgml/faq-not.txt>

| Q. How do I include JavaScript inside an XML document?
|
| A. Easy! Just write:
|
| <code notation="JavaScript"><![CDATA[
| ... your JavaScript code goes here ...
| ... make sure it doesn't contain the sequence "]]>"...
| ]]>
| </code>
|
| Note that this solution also works for Perl, Python, Tcl,
| REXX, Icon, Ada, Basic, Beta, C, C++, Eiffel, Forth, Fortran,
| Haskell, Scheme, SML, Pascal, Modula, PL/I, Prolog, REXX, Sather,
| Smalltalk, SNOBOL, RPG/III, and COBOL.
|
| But not APL. Sorry.
|
| Q. But that doesn't work!
|
| A. What do you mean it doesn't work? There's your XML document,
| there's your JavaScript, there's your JavaScript inside your
| XML document, just like you asked.
 
D

Denis McMahon

[stuff about javascript wrapping in xml]

I'm sure all that stuff about xml is interesting, but I'm at a loss as to
how it relates to my validated html 4.01 strict solution to the OPs html
(I assume) problem.

Rgds

Denis McMahon
 
D

Denis McMahon

How does that relate to ‘should not’, and did you see the FAQ?

OK, so I hadn't read the faq, and was unaware of the "shouldn't use the
javascript pseudo-protocol / schema" instruction.

I've now revised the page, it still validates and works in my firefox:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=us-ascii">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Bill Steele's Broken Code - Fixed (better)</title>
<script type="text/javascript">
function c ()
{
var c, n, t, o;
c = 'He said "Hello, World!"';
n = document.createElement("p");
t = document.createTextNode(c);
n.appendChild(t);
o = document.getElementById("foo").getElementsByTagName("p")[0];
document.getElementById("foo").removeChild(o);
document.getElementById("foo").appendChild(n);
}
</script>
<style type="text/css">
span.link {color:blue;text-decoration:underline;cursor:pointer}
span.link:hover {color:blue}
span.link:active {color:red}
</style>
</head>
<body>
<div id="foo">
<p><span onclick="c()" class="link">What did he say?</span> The
book says the double quotes are fine inside single quotes.
In practice they act as the end of the script. Escaping
them doesn't work. (Escaping single quotes and apostrophes
does work.)</p>
</div>
</body>
</html>

same url, <url:http://www.sined.co.uk/tmp/billsteele.htm>

Rgds

Denis McMahon
 
E

Eric Bednarz

Denis McMahon said:
[stuff about javascript wrapping in xml]

I'm sure all that stuff about xml is interesting, but I'm at a loss as to
how it relates to my validated html 4.01 strict solution to the OPs html
(I assume) problem.

XML- or SGML-based, markup parsing does not know about script, and the
fact that using the ‘javascript’ pseudo-protocol ‘validates’ in the
context of a CDATA context model is quite irrelevant (admittedly, the
cited document is probably only funny if you know too much about SGML).
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top