quotes in innerHTML

Discussion in 'Javascript' started by Bill Steele, Aug 3, 2011.

  1. Bill Steele

    Bill Steele Guest

    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. )
     
    Bill Steele, Aug 3, 2011
    #1
    1. Advertisements

  2. This script is not going to run in that (X)HTML document; the `script'
    element is missing.
    You have forgotten the TAGC delimiter, `>'.

    You should not use `javascript:', see the FAQ.
    Which book?
    They are, and vice-versa.
    No, they don't. On top of your syntactically invalid code you have mistyped
    `getElementById'. These are case-sensitive programming languages.
    Because that is very likely not the cause of your problem.
    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
     
    Thomas 'PointedEars' Lahn, Aug 3, 2011
    #2
    1. Advertisements

  3. Bill Steele

    Andy Guest

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

    Andy
     
    Andy, Aug 4, 2011
    #3
  4. Bill Steele

    SAM Guest

    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!" '

    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>
     
    SAM, Aug 4, 2011
    #4
  5. 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
     
    Denis McMahon, Aug 4, 2011
    #5
  6. Bill Steele

    Eric Bednarz Guest

    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.
     
    Eric Bednarz, Aug 4, 2011
    #6
  7. 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
     
    Denis McMahon, Aug 4, 2011
    #7
  8. 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
     
    Denis McMahon, Aug 4, 2011
    #8
  9. Bill Steele

    Eric Bednarz Guest

    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).
     
    Eric Bednarz, Aug 4, 2011
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.