Javascript intrepreting an apostrophe as a delimiter while displaying a perl variable

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

    Javascript intrepreting an apostrophe as a delimiter while displaying a perl variable

    I am working on a cgi script that is suppose to pop-up a javascript
    box from the following perl variables:$Toda yDate, $LinkCity,
    $LinkState. I recently encountered a problem with the $LinkCity
    variable when the city Coeur d'Alene was read. It appears that
    javascript is intrepreting the quote to end after d'. I've tried
    reversing the quotes (instead of " ' ' ", ' " " ') and using the
    javascript escape command. Neither worked. Please help!

    example:
    if ($AQI1 eq "Good") {
    $message = &altQualityMess ages($Pollutant {$TodayDay . $City .
    $State},"Good") ;
    $AQI1="<A HREF=\"javascri pt:PopUp(\'$Tod ayDate\',\'$Lin kCity\',\'$Link State\')\"
    onMouseOver=\"w indow.status='$ Pollutant{$Toda yDay . $City . $State}
    Forecast For $City, $State';return true\"



    javascript error:
    missing ; before statement.
    window.status=' OZONE Forecast For Coeur d'Alene, ID';return true
  • Markus Ernst

    #2
    Re: Javascript intrepreting an apostrophe as a delimiter while displaying a perl variable

    I had a similar problem with a PHP mailing tool I wrote where apostrophes in
    a text interfered with the apostrophes in the form elements (value="$text") .
    I made a workaround with urlencoding and urldecoding the string.

    One problem with this is that if you urlencode with Perl it is not sure that
    the decodeURI function in javascript decodes it properly, you have to test
    that (I am not familiar with Pearl, it does not work with PHP). I would try
    adding a script in the HTML head, the output should look something like:

    var jsLinkCity = encodeURI($Link City)

    and then pass the variable jsLinkCity with the Popup function and decode it
    again in the head of the popup window file.

    You can also encode and decode it with Perl, but probably not encode it with
    one and decode it with the other.

    hth
    Markus


    "Jeanne" <browdy.jeanne@ epa.gov> schrieb im Newsbeitrag
    news:61604a16.0 306300608.4551c ab@posting.goog le.com...[color=blue]
    > I am working on a cgi script that is suppose to pop-up a javascript
    > box from the following perl variables:$Toda yDate, $LinkCity,
    > $LinkState. I recently encountered a problem with the $LinkCity
    > variable when the city Coeur d'Alene was read. It appears that
    > javascript is intrepreting the quote to end after d'. I've tried
    > reversing the quotes (instead of " ' ' ", ' " " ') and using the
    > javascript escape command. Neither worked. Please help!
    >
    > example:
    > if ($AQI1 eq "Good") {
    > $message = &altQualityMess ages($Pollutant {$TodayDay . $City .
    > $State},"Good") ;
    > $AQI1="<A[/color]
    HREF=\"javascri pt:PopUp(\'$Tod ayDate\',\'$Lin kCity\',\'$Link State\')\"[color=blue]
    > onMouseOver=\"w indow.status='$ Pollutant{$Toda yDay . $City . $State}
    > Forecast For $City, $State';return true\"
    >
    > http://www.epa.gov/cgi-bin/airnow.cg...splay=FORECAST
    >
    > javascript error:
    > missing ; before statement.
    > window.status=' OZONE Forecast For Coeur d'Alene, ID';return true[/color]



    Comment

    • Daniel

      #3
      Re: Javascript intrepreting an apostrophe as a delimiter while displaying a perl variable

      > var jsLinkCity = encodeURI($Link City)[color=blue]
      >
      > and then pass the variable jsLinkCity with the Popup function and decode[/color]
      it[color=blue]
      > again in the head of the popup window file.
      >
      > You can also encode and decode it with Perl, but probably not encode it[/color]
      with[color=blue]
      > one and decode it with the other.
      >[/color]

      Actually, I don't think it would be a good idea to URL encode it, since the
      only characters that'd stress out Javascript in a variable value would be
      apostrophe and in certain cases backslash (like if for some reason a value
      contained (and was supposed to contain) \' or \\ or \n). If you just use cgi
      to replace every instance of \ with \\ and ' with \' your Javascript
      variables will be correct (at least AFAIK ;)

      Daniel


      Comment

      • Douglas Crockford

        #4
        Re: Javascript intrepreting an apostrophe as a delimiter while displaying a perl variable

        > I am working on a cgi script that is suppose to pop-up a javascript[color=blue]
        > box from the following perl variables:$Toda yDate, $LinkCity,
        > $LinkState. I recently encountered a problem with the $LinkCity
        > variable when the city Coeur d'Alene was read. It appears that
        > javascript is intrepreting the quote to end after d'. I've tried
        > reversing the quotes (instead of " ' ' ", ' " " ') and using the
        > javascript escape command. Neither worked. Please help!
        >
        > example:
        > if ($AQI1 eq "Good") {
        > $message = &altQualityMess ages($Pollutant {$TodayDay . $City .
        > $State},"Good") ;
        > $AQI1="<A[/color]
        HREF=\"javascri pt:PopUp(\'$Tod ayDate\',\'$Lin kCity\',\'$Link State\')\"[color=blue]
        > onMouseOver=\"w indow.status='$ Pollutant{$Toda yDay . $City . $State}
        > Forecast For $City, $State';return true\"[/color]

        It bites that JavaScript does not have nesting string delimiters. Rebol does
        (www.rebol.com), and I think that is quite civilized.

        You need a slashify function which will insert a backslash before every quote,
        single quote, and backslash in a string. Using such a function, your code will
        get a lot easier to read, too.



        Comment

        Working...