Javascript escaping madness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucian Sandor

    #1

    Javascript escaping madness

    Hello everyone,
    While I'm a newbie here, I a not new to google, so please don't send
    me back, it would be useless.
    First of all I have to specify I am working on a Blogger.com template,
    therefore anything I'll write should be stuck on a single file.
    I thought about creating a funny pop-up.
    <a href="javascrip t:myFunction(); "...
    that's because, as you will see, the code for the popup is pretty
    complex.
    I've insterted myFunction somewhere in the file.
    First question: according to the w3c validator, the only way to
    include complex script, including string variables with HTML tags in
    them, is to insert HTML comments?
    Then, I've started by building a window caled myPopup and then
    myPopup.documen t.write("<html> ");...
    The second question: is there a recommended way to insert string
    constants in inline scripts? (AFAIK single quotes were recommended,
    w3c also choked on double quotes, Mozilla does not like double quotes
    too)
    Then we had something more complex: html tags with attributes. I had
    managed to insert them using escaping character such as
    myPopup.documen t.write(' <p style= \' align:top \' > ');
    But next, on the popup I had to inject some fancy inline JS code.
    Third question: how would I, following the document.write way, insert
    in the popup a code like:
    <a href="#" onclick="if(doc ument.all){bRes ult =
    window.clipboar dData.setData(' Text',document. selection.creat eRange().text;} ">
    or maybe
    <a href='#' onclick='if(doc ument.all){bRes ult =
    window.clipboar dData.setData(" Text",document. selection.creat eRange().text;} '>
    Fourth, is there any difference between the two previous anchors in
    the way they are written? Is one of the two forms recomended?
    (somehow, it is the same question with the first one)
    As you can see, two types of quotes are needed, and they should be
    written through document.write. Is that even possible?
    TIA,
    LS.
  • Michael Winter

    #2
    Re: Javascript escaping madness

    On 30 Jul 2004 15:38:29 -0700, Lucian Sandor <lucisandor@gma il.com> wrote:
    [color=blue]
    > Hello everyone,
    > While I'm a newbie here, I a not new to google, so please don't send
    > me back, it would be useless.
    > First of all I have to specify I am working on a Blogger.com template,
    > therefore anything I'll write should be stuck on a single file.
    > I thought about creating a funny pop-up.
    > <a href="javascrip t:myFunction(); "...[/color]

    Don't use this form. It causes several problems, not least of which
    results in a page that is completely unusably by users that disable
    JavaScript.

    <URL:http://www.jibbering.c om/faq/#FAQ4_24>
    [color=blue]
    > that's because, as you will see, the code for the popup is pretty
    > complex.
    > I've insterted myFunction somewhere in the file.
    > First question: according to the w3c validator, the only way to
    > include complex script, including string variables with HTML tags in
    > them, is to insert HTML comments?[/color]

    This "error" is documented by the validator:

    <URL:http://validator.w3.or g/docs/help.html#faq-javascript>

    Basically, the validator interprets "</" anywhere within the block as the
    end of the SCRIPT element, even if the closing tag is not for a SCRIPT
    element (browsers are allowed to do this!) When it examines the tag more
    closely, it discovers that is not "</SCRIPT>" as it expects, hence the

    end tag for element "..." which is not open

    message. The solution, included in the validator's FAQ *and* the HTML
    specification, is to escape the forward slash. That is, change

    var myHTML = '<a href="myPage.ht ml">Link</a>';

    to

    var myHTML = '<a href="myPage.ht ml">Link<\/a>';

    Whilst this has no effect on the script (in that regard, the lines are
    identical), the HTML parser won't see "</".

    Another solution is simply to remove the embedded script and place it in a
    separate file.
    [color=blue]
    > Then, I've started by building a window caled myPopup and then
    > myPopup.documen t.write("<html> ");...[/color]

    It might be easier to create a skeleton HTML file, dynamically modifying
    that, rather than creating it from scratch. Writing valid HTML (something
    you should always endeavour to do) purely with document.write( ) calls is a
    waste of time.
    [color=blue]
    > The second question: is there a recommended way to insert string
    > constants in inline scripts? (AFAIK single quotes were recommended,
    > w3c also choked on double quotes, Mozilla does not like double quotes
    > too)[/color]

    You'll have to explain what you mean here. Define "insert string
    constants" and your idea of "inline scripts". My notion of the latter is a
    SCRIPT element with the code embedded, as opposed to a SCRIPT element with
    the src attribute. Perhaps you mean intrinsic events? I'll assume that for
    the moment.

    If you need to include a double quote (") within a HTML attribute value,
    you need to use entity references, just as you would with normal text:

    <a onclick="myWin. document.write( &quot;Some text&quot;);ret urn false">

    To the JavaScript engine, this would be interpreted as:

    myWin.document. write("Some text");return false
    [color=blue]
    > Then we had something more complex: html tags with attributes. I had
    > managed to insert them using escaping character such as
    > myPopup.documen t.write(' <p style= \' align:top \' > ');[/color]

    Just covered that.
    [color=blue]
    > But next, on the popup I had to inject some fancy inline JS code.[/color]

    Again, a shaky definition - "inline" - so I'll still assume intrinsic
    events.
    [color=blue]
    > Third question: how would I, following the document.write way, insert
    > in the popup a code like:
    > <a href="#" onclick="if(doc ument.all){bRes ult =
    > window.clipboar dData.setData(' Text',document. selection.creat eRange().text;} ">
    > or maybe
    > <a href='#' onclick='if(doc ument.all){bRes ult =
    > window.clipboar dData.setData(" Text",document. selection.creat eRange().text;} '>[/color]

    I wouldn't recommend including such large amounts of code directly inside
    an intrinsic event. You should limit the contents to a few simple
    statements. Anything more complicated should be wrapped in a function. It
    aids in debugging as you don't have to search a single, massive line. You
    also avoid this whole debacle because there will be no nested strings
    (they'll be in the SCRIPT block).

    If you do want to persist, use entity references as I explained above.
    [color=blue]
    > Fourth, is there any difference between the two previous anchors in
    > the way they are written? Is one of the two forms recomended?
    > (somehow, it is the same question with the first one)
    > As you can see, two types of quotes are needed, and they should be
    > written through document.write. Is that even possible?[/color]

    If you've taken my advice on board, these final questions shouldn't matter
    anymore. However, I would stick to whatever format you use in your HTML.

    Personally, I always use double quotes for HTML attributes, and single
    quotes for JavaScript strings. This allows me to place double quotes
    inside strings (which I do more often than apostrophes), and place strings
    inside attribute values. That is:

    '<a href="myPage.ht ml">Link<\/a>'

    and

    onclick="showTi tle('Introducti on')"

    If I needed to do both, you'd have to escape the internal double quotes as
    character entities, irrespective of which quote type was outer- or
    innermost.

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply

    Comment

    • Lucian Sandor

      #3
      Re: Javascript escaping madness

      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsby5bnq cx13kvk@atlanti s>...[color=blue]
      > On 30 Jul 2004 15:38:29 -0700, Lucian Sandor <lucisandor@gma il.com> wrote:
      >[color=green]
      > > Hello everyone,
      > > While I'm a newbie here, I a not new to google, so please don't send
      > > me back, it would be useless.
      > > First of all I have to specify I am working on a Blogger.com template,
      > > therefore anything I'll write should be stuck on a single file.
      > > I thought about creating a funny pop-up.
      > > <a href="javascrip t:myFunction(); "...[/color]
      >
      > Don't use this form. It causes several problems, not least of which
      > results in a page that is completely unusably by users that disable
      > JavaScript.
      >
      > <URL:http://www.jibbering.c om/faq/#FAQ4_24>
      >[/color]
      I am aware of this recommendation, but this is a Blogger.com specific
      issue. I would be glad to separate JS, HTML and CSS files, but this
      option isn't available.
      .....
      [color=blue][color=green]
      > > The second question: is there a recommended way to insert string
      > > constants in inline scripts? (AFAIK single quotes were recommended,
      > > w3c also choked on double quotes, Mozilla does not like double quotes
      > > too)[/color]
      >
      > You'll have to explain what you mean here. Define "insert string
      > constants" and your idea of "inline scripts". My notion of the latter is a
      > SCRIPT element with the code embedded, as opposed to a SCRIPT element with
      > the src attribute. Perhaps you mean intrinsic events? I'll assume that for
      > the moment.[/color]
      You are right, "inline" should be understood as scripts inserted in
      the HTML file, as opposed to separate JS files.
      [color=blue]
      > If you need to include a double quote (") within a HTML attribute value,
      > you need to use entity references, just as you would with normal text:
      >
      > <a onclick="myWin. document.write( &quot;Some text&quot;);ret urn false">
      >
      > To the JavaScript engine, this would be interpreted as:
      >
      > myWin.document. write("Some text");return false
      >[/color]

      This is a good idea. I see already that the script is running already
      in IE.

      ....[color=blue]
      > Hope that helps,
      > Mike[/color]
      It helped indeed. Thank you.
      Regards,
      LS.

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Javascript escaping madness

        Lucian Sandor wrote:[color=blue]
        > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsby5bnq cx13kvk@atlanti s>...[/color]

        Please do not write attribution novels.
        [color=blue][color=green]
        >> On 30 Jul 2004 15:38:29 -0700, Lucian Sandor <lucisandor@gma il.com> wrote:[color=darkred]
        >> > Hello everyone,
        >> > While I'm a newbie here, I a not new to google, so please don't send
        >> > me back, it would be useless.
        >> > First of all I have to specify I am working on a Blogger.com template,
        >> > therefore anything I'll write should be stuck on a single file.
        >> > I thought about creating a funny pop-up.
        >> > <a href="javascrip t:myFunction(); "...[/color]
        >>
        >> Don't use this form. It causes several problems, not least of which
        >> results in a page that is completely unusably by users that disable
        >> JavaScript.
        >>
        >> <URL:http://www.jibbering.c om/faq/#FAQ4_24>
        >>[/color]
        > I am aware of this recommendation, but this is a Blogger.com specific
        > issue. I would be glad to separate JS, HTML and CSS files, but this
        > option isn't available.
        > ....[/color]

        You probably want

        <script type="text/javascript">
        document.write(
        '<a href="#" onclick="myFunc tion(); return false">...<\/a>');
        </script>

        then.
        [color=blue][color=green]
        >> If you need to include a double quote (") within a HTML attribute value,
        >> you need to use entity references, just as you would with normal text:
        >>
        >> <a onclick="myWin. document.write( &quot;Some text&quot;);ret urn false">
        >>
        >> To the JavaScript engine, this would be interpreted as:
        >>
        >> myWin.document. write("Some text");return false[/color]
        >
        > This is a good idea. I see already that the script is running already
        > in IE.[/color]

        You should include a non-empty "href" attribute as well. An "a" element
        without both the "href" attribute and one of "id" or "name" is just
        nothing -- no hyperlink, no anchor. There are user agents which will
        ignore the event handler because of this. You should always test your
        scripts/documents with more than one user agent. If it works in IE, it
        is simply not enough, because IE makes gold of every sh*t.


        PointedEars

        Comment

        • Dr John Stockton

          #5
          Re: Javascript escaping madness

          JRS: In article <410BC456.80306 07@PointedEars. de>, dated Sat, 31 Jul
          2004 18:09:58, seen in news:comp.lang. javascript, Thomas 'PointedEars'
          Lahn <PointedEars@nu rfuerspam.de> posted :[color=blue]
          >Lucian Sandor wrote:[color=green]
          >> "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsby[/color]
          >5bnqcx13kvk@at lantis>...
          >
          >Please do not write attribution novels.[/color]

          Fewmets.

          Read
          Read more about the Internet Engineering Task Force (IETF), the premier standards development organization (SDO) for the Internet.

          Read more about the Internet Engineering Task Force (IETF), the premier standards development organization (SDO) for the Internet.

          which indicate current thinking.

          Lahn puts a naive degree of credence in a local document which is
          without general authority, and in his own importance.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
          Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
          Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
          No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

          Comment

          • Randy Webb

            #6
            Re: Javascript escaping madness

            Thomas 'PointedEars' Lahn babbled incoherently while quoting an
            attribution line which included no extraneous information:
            [color=blue]
            > Lucian Sandor wrote:
            >[color=green]
            >>"Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsby5bnq cx13kvk@atlanti s>...[/color]
            >
            >
            > Please do not write attribution novels.[/color]

            Please stop your whining about inconsequential things, it detracts from
            what you actually do know.

            Comment

            Working...