Escaping " and ' characters in Javascript

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

    #1

    Escaping " and ' characters in Javascript


    I am using PHP to generate a little javascript for one of my pages. In
    short it allows a user to select a value from a list and pop it into a form
    field on a seperate page. This works well unless there is a " or ' in the
    character string.

    <SCRIPT language=JavaSc ript>
    function Add_Term(Search Term) {
    window.opener.d ocument.advsear ch.name_title.v alue +=
    SearchTerm;
    window.close();
    }
    </SCRIPT>

    <a href="javascrip t:Add_Term('Thi s will work')">"Amarga Presencia"</a>

    <a href="javascrip t:Add_Term('Thi s "won't" work')">"Amarga Presencia"</a>

    How do I escape the " and ' characters properly ..? I know this is basic
    but I have tried \' and \" and cant think what else to do.


    --

    NecroJoe
    -- US East : KCarpenterRox / Necro / 73


  • Lasse Reichstein Nielsen

    #2
    Re: Escaping &quot; and ' characters in Javascript

    I
    "NecroJoe" <TAKEjamesOUTst arrittTOMAIL@ho tmail.com> writes:
    [color=blue]
    > <SCRIPT language=JavaSc ript>[/color]

    In HTML4, the type attribute is required, and it is always sufficient.
    Use:
    <script type="text/javascript">
    [color=blue]
    > function Add_Term(Search Term) {
    > window.opener.d ocument.advsear ch.name_title.v alue +=
    > SearchTerm;
    > window.close();
    > }
    > </SCRIPT>
    >
    > <a href="javascrip t:Add_Term('Thi s will work')">"Amarga Presencia"</a>[/color]

    Don't use "javascript :"-URLs. <URL:http://jibbering.com/faq/#FAQ4_24>
    [color=blue]
    > <a href="javascrip t:Add_Term('Thi s "won't" work')">"Amarga Presencia"</a>[/color]

    No, the HTML attribute is surrounded by "'s, so you can't use them inside
    it as well. Use:
    ---
    <a href="javascrip t:Add_Term('Thi s &quot;won't&quo t; work')"> ...
    ---
    (or rather:
    ---
    <a href="needJS.ht ml" onclick="Add_Te rm('This &quot;won't&quo t; work')"> ...
    ---
    )
    [color=blue]
    > How do I escape the " and ' characters properly ..?[/color]

    What you need is the HTML escape, which is &quot; for " and &apos; for '.
    If you also need to nest quotes inside the Javascript, then use backslashes
    to escape:
    ----
    onclick="foo('a in\'t this &quot;kosher&qu ot;?');
    bar(&quot;No \&quot;dear\&qu ot;!&quot;);"
    ---
    This is HTML escape for this Javascript:
    ---
    foo('ain\'t this "kosher"?') ;
    bar("No \"dear\"!");
    ---
    [color=blue]
    > I know this is basic
    > but I have tried \' and \" and cant think what else to do.[/color]

    HTML escapes, not Javascript. :)
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • NecroJoe

      #3
      Re: Escaping &quot; and ' characters in Javascript

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:brj3p653.f sf@hotpop.com.. .
      [color=blue]
      > Don't use "javascript :"-URLs. <URL:http://jibbering.com/faq/#FAQ4_24>
      >[color=green]
      > > <a href="javascrip t:Add_Term('Thi s "won't" work')">"Amarga[/color][/color]
      Presencia"</a>[color=blue]
      >
      > No, the HTML attribute is surrounded by "'s, so you can't use them inside
      > it as well. Use:
      > ---
      > <a href="javascrip t:Add_Term('Thi s &quot;won't&quo t; work')"> ...
      > ---
      > (or rather:
      > ---
      > <a href="needJS.ht ml" onclick="Add_Te rm('This &quot;won't&quo t; work')">[/color]
      ....[color=blue]
      > ---
      > )
      >[color=green]
      > > How do I escape the " and ' characters properly ..?[/color]
      >
      > What you need is the HTML escape, which is &quot; for " and &apos; for '.
      > If you also need to nest quotes inside the Javascript, then use[/color]
      backslashes[color=blue]
      > to escape:
      > ----
      > onclick="foo('a in\'t this &quot;kosher&qu ot;?');
      > bar(&quot;No \&quot;dear\&qu ot;!&quot;);"
      > ---
      > This is HTML escape for this Javascript:
      > ---
      > foo('ain\'t this "kosher"?') ;
      > bar("No \"dear\"!");
      > ---
      >[color=green]
      > > I know this is basic
      > > but I have tried \' and \" and cant think what else to do.[/color]
      >
      > HTML escapes, not Javascript. :)
      > /L[/color]

      I would have snipped a bit more but that is good stuff ;-D -- thanks a lot,
      I appreciate it.

      --

      NecroJoe
      -- US East : KCarpenterRox / Necro / 73


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Escaping &quot; and ' characters in Javascript

        Lasse Reichstein Nielsen wrote:[color=blue]
        > "NecroJoe" <TAKEjamesOUTst arrittTOMAIL@ho tmail.com> writes:[color=green]
        >><a href="javascrip t:Add_Term('Thi s "won't" work')">"Amarga Presencia"</a>[/color]
        >
        >
        > No, the HTML attribute is surrounded by "'s, so you can't use them inside
        > it as well. Use:
        > ---
        > <a href="javascrip t:Add_Term('Thi s &quot;won't&quo t; work')"> ...
        > ---
        > (or rather:
        > ---
        > <a href="needJS.ht ml" onclick="Add_Te rm('This &quot;won't&quo t; work')"> ...[/color]

        Correct, either one won't work ;-)

        Use

        <a href="javascrip t:Add_Term('Thi s &quot;won\'t&qu ot; work')"> ...

        or rather

        <a href="needJS.ht ml"
        onclick="Add_Te rm('This &quot;won\'t&qu ot; work')"> ...

        instead.


        PointedEars
        --[color=blue]
        > does anyone have any info about how can i make an explorer type menu?[/color]
        Care to be more specific? Windows Explorer? Internet Explorer? Jungle
        Explorer? (Fabian in cljs, <brk1e1$46tab$1 @ID-174912.news.uni-berlin.de>)

        Comment

        Working...