novice help! (single quote escaping)

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

    novice help! (single quote escaping)

    hi, i can't seem to be able to escape my single quote properly... is it
    even possible in javascript?

    this is a portion of my code..

    var DLEWIS="Pastor Lewis";

    .... Sermon is a yser-defined class ..

    var en_20031102=new Sermon("11/02",DLEWIS,"T he Lord\'s Supper: The Art Of
    Struggle",
    "<a
    href='http://www.biblegatewa y.com/cgi-bin/bible?language= english&passage =Lam
    entations+3%3A1 9-27&version=NIV ' target='_blank' >Lamentations<b r />
    3:19-27</a>",
    "sermon-20031102-en.m3u","-");


    .... and then in the printing function i write the following code:

    document.write( "<a href='"+sermonA rray[i].notes+"'><img src='ppt.gif'
    alt='");
    document.write( sermonArray[i].title+" - "+sermonArr ay[i].speaker+" ||
    "+sermonArr ay[i].date+"' /></a>");

    for a sample of how it looks, you can go check out:

    is anyone able to give me advice? thanks.,.


  • Yann-Erwan Perio

    #2
    Re: novice help! (single quote escaping)

    sankofa wrote:
    [color=blue]
    > hi, i can't seem to be able to escape my single quote properly... is it
    > even possible in javascript?[/color]
    [color=blue]
    > var en_20031102=new Sermon("11/02",DLEWIS,"T he Lord\'s Supper: The Art Of[/color]

    No need to escape the single quote here.
    [color=blue]
    > document.write( "<a href='"+sermonA rray[i].notes+"'><img src='ppt.gif'
    > alt='");[/color]

    So you're using single quotes for the attribute quote, and you do have
    single quotes in the string being written like in...
    [color=blue]
    > document.write( sermonArray[i].title+" - "+sermonArr ay[i].speaker+" ||
    > "+sermonArr ay[i].date+"' /></a>");[/color]

    .... so the quick solution would be to use escaped double quotes for the
    HTML attribute quote, starting at

    document.write( "<a href=\""+...

    More generally, when writing HTML with javascript, you have to determine
    whether you need javascript escapes or HTML escapes.

    Basically, you can start from the output (HTML) to build your strings.
    If you want something like

    <tag attr="'foo'">

    then you'll have to write something like

    document.write( "<tag attr=\"'foo'\"> ");

    escaping the double quotes for the attributes for javascript. Inverting
    the logic with single quotes work the same way.

    Now if you need to have both single and double quotes in the quoted
    attribute, your problem is clearly related with the HTML and the quoting
    of the attribute, so you'll have to use HTML "escapes", i.e. characters
    entities, for instance a desired output could be

    <tag title="Hello 'World' and &quot;foo&quot; ">

    and the related javascript would be like

    document.write( "<tag title=\"Hello 'World' and &quot;foo&quot; \">");

    Generally, problems are raised when concatenating strings many times in
    javascript; the level of escape increases each time, with makes the code
    difficult to maintain. The solution is to maximize the use of
    references, concatenating only once.


    HTH
    Yep.

    Comment

    • Fabian

      #3
      Re: novice help! (single quote escaping)

      Yann-Erwan Perio hu kiteb:
      [color=blue]
      > document.write( "<tag attr=\"'foo'\"> ");
      >
      > escaping the double quotes for the attributes for javascript.
      > Inverting the logic with single quotes work the same way.[/color]

      This doesn't strictly follow standards, but every browser I know of will
      accept an attribute that has no quote proided that the attribute has no
      spaces.


      --
      --
      Fabian
      Visit my website often and for long periods!


      Comment

      • HikksNotAtHome

        #4
        Re: novice help! (single quote escaping)

        In article <bokemt$1f2n90$ 1@ID-174912.news.uni-berlin.de>, "Fabian"
        <lajzar@hotmail .com> writes:
        [color=blue]
        >Yann-Erwan Perio hu kiteb:
        >[color=green]
        >> document.write( "<tag attr=\"'foo'\"> ");
        >>
        >> escaping the double quotes for the attributes for javascript.
        >> Inverting the logic with single quotes work the same way.[/color]
        >
        >This doesn't strictly follow standards, but every browser I know of will
        >accept an attribute that has no quote proided that the attribute has no
        >spaces.[/color]

        Can you space that attr= part out and tell me which part of it was not quoted?
        In fact, it was *double* quoted:

        document.write( "<tag attr= \ " ' foo ' \ " >");

        Which writes this:
        <tag attr=" ' foo ' " >

        --
        Randy

        Comment

        • Dr John Stockton

          #5
          Re: novice help! (single quote escaping)

          JRS: In article <3fad6e9f$0$277 5$626a54ce@news .free.fr>, seen in
          news:comp.lang. javascript, Yann-Erwan Perio <y-e.perio@em-lyon.com>
          posted at Sat, 8 Nov 2003 23:31:58 :-[color=blue]
          >
          ><tag title="Hello 'World' and &quot;foo&quot; ">
          >
          >and the related javascript would be like
          >
          > document.write( "<tag title=\"Hello 'World' and &quot;foo&quot; \">");
          >
          >Generally, problems are raised when concatenating strings many times in
          >javascript; the level of escape increases each time, with makes the code
          >difficult to maintain. The solution is to maximize the use of
          >references, concatenating only once.[/color]


          That may cause confusion; you are using 'concatenation' to mean
          'nesting'.

          Concatenation : "pig" + 'tail'
          Nesting : "n'oeuf'id" or "n\"oeuf\"i d"

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          Working...