Problem With String Containing JavaScript Escape Character

Discussion in 'ASP .Net' started by Jeff S, Nov 5, 2003.

  1. Jeff S

    Jeff S Guest

    In a VB.NET code behind module, I build a string for a link that points to a
    JavaScript function. The two lines of code below show what is relevant.

    PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34)
    & ", " & Chr(34) & CurrentEventDetails & ")"
    strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
    "</A><BR>"

    The problem I have is that when the string variables [PopUpWindowTitle] or
    [CurrentEventDetails] contain a string with an apostrophe, then the server
    throws an error "Unterminated String Constant". This error shows up prior to
    the page rendering in the browser (or as the page is being rendered to the
    browser - I'm not sure which - but it's before the page shows up in the
    browser).

    I tried including JavaScript's "\" escape character - but this does not
    solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "'",
    "\' "). The server still throws the Unterminated String Constant error.

    What can I do about this? I need to include apostrophes in the JavaScript
    function call string and VB.NET is ignoring JavaScript's Escape
    character -yet honoring JavaScript's syntax rules.

    Thanks in advance.
    Jeff S, Nov 5, 2003
    #1
    1. Advertising

  2. Jeff S

    mikeb Guest

    Jeff S wrote:
    > In a VB.NET code behind module, I build a string for a link that points to a
    > JavaScript function. The two lines of code below show what is relevant.
    >
    > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34)
    > & ", " & Chr(34) & CurrentEventDetails & ")"
    > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
    > "</A><BR>"
    >
    > The problem I have is that when the string variables [PopUpWindowTitle] or
    > [CurrentEventDetails] contain a string with an apostrophe, then the server
    > throws an error "Unterminated String Constant". This error shows up prior to
    > the page rendering in the browser (or as the page is being rendered to the
    > browser - I'm not sure which - but it's before the page shows up in the
    > browser).
    >
    > I tried including JavaScript's "\" escape character - but this does not
    > solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "'",
    > "\' "). The server still throws the Unterminated String Constant error.
    >
    > What can I do about this? I need to include apostrophes in the JavaScript
    > function call string and VB.NET is ignoring JavaScript's Escape
    > character -yet honoring JavaScript's syntax rules.
    >
    > Thanks in advance.
    >
    >


    Are you really getting the error on the server, not the client?

    I can see a couple possible problems:

    1) in your 2 line sample above, you are missing a Chr(34) term in the
    expression that builds PopupLink. it seems that the end of that
    expression should be:

    CurrentEventDetails & Chr(34) & ")"

    not:

    CurrentEventDetails & ")"

    since you presumably need a closing double quote for the
    CurrentEventDetails parameter to the client side PopUpWindow() function.

    I'd be willing to bet that the missing Chr(34) expression is in your
    code, it's probably just a typo in your post - <soapbox> to which I say,
    "cut-n-paste is your friend". It's inexcusable to have typos in code
    snippets that can be pasted from the real code. I mean, if you have a
    typo in your snippet that is supposed to demonstrate the problem, how
    are the readers supposed to know what the problem really is? </soapbox>

    2) this is what I believe to be your real problem. Instead of:

    Replace(PopUpWindowTitle, "'","\' ")

    try using:

    Replace(PopUpWindowTitle, "'","\x27")

    or:

    Replace(PopUpWindowTitle, "'","'")


    The browser doesn't do JavaScript-style handling of escape sequences -
    it uses Character Entities to escape special characters. And the
    browser obviously needs to process the page before it can pass bits of
    the page off to the JavaScript processor.

    If you replace apostrophes with "\x27", that string of characters means
    nothing to the browser, so that's what gets passed in to the JavaScript
    processor, which sees it as an escape sequence for the apostrophe.

    If you replace with "'", that string of characters is an 'Character
    Entity' which the browser translates into an apostrophe before passing
    the string on to the JavaScript processor.

    Sorry for the long post.
    --
    mikeb
    mikeb, Nov 5, 2003
    #2
    1. Advertising

  3. Jeff S

    psb Guest

    response.write ("this is a double "" quote")

    output = this is a double " quote




    "Jeff S" <> wrote in message
    news:...
    > In a VB.NET code behind module, I build a string for a link that points to

    a
    > JavaScript function. The two lines of code below show what is relevant.
    >
    > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &

    Chr(34)
    > & ", " & Chr(34) & CurrentEventDetails & ")"
    > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
    > "</A><BR>"
    >
    > The problem I have is that when the string variables [PopUpWindowTitle] or
    > [CurrentEventDetails] contain a string with an apostrophe, then the server
    > throws an error "Unterminated String Constant". This error shows up prior

    to
    > the page rendering in the browser (or as the page is being rendered to the
    > browser - I'm not sure which - but it's before the page shows up in the
    > browser).
    >
    > I tried including JavaScript's "\" escape character - but this does not
    > solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "'",
    > "\' "). The server still throws the Unterminated String Constant error.
    >
    > What can I do about this? I need to include apostrophes in the JavaScript
    > function call string and VB.NET is ignoring JavaScript's Escape
    > character -yet honoring JavaScript's syntax rules.
    >
    > Thanks in advance.
    >
    >
    psb, Nov 5, 2003
    #3
  4. Jeff S

    Jeff S Guest

    Apparently you saw right through my little ploy to post simpler code to the
    newsgroup than what I was actually working with (and with the omission/typo
    you correctly concluded). Your <soapbox> points are well taken...

    I proceeded to go with your suggestions, but I still get the error. So, I
    posted the complete section of code below.

    Additionally, on closer observation, the sequence that leads to the error
    message appearing is as follows:
    1. From the VS.NET IDE, I press F5 to run the project (with the ASPX page in
    question set as the start page).
    2. The browser window appear, and I see the top part of the ASPX page
    rendered (e.g., the page header graphics show up).
    3. The messagebox appears displaying the "Error: Unterminated string
    constant" message.
    4. At this point, only the top part of the page is rendered, and the message
    box is displayed in the middle of the browser window. If I click on either
    Yes or No (the only two buttons on the message box - prompting to debug or
    not), the page does finish rendering in the browser. The page looks fine,
    but the links are dead (presumably because the JavaScript function call part
    of the link is invalid).

    Here's the code (followed by runtime sample values):

    For Each CalendarEntry In m_CalendarEntries
    If CalendarEntry.EventDateTime = DateToGet Then

    'Get the for the JS popup window ready
    CurrentDate = CStr(CalendarEntry.EventDateTime)
    CurrentEventDetails = CalendarEntry.LongDescription
    CurrentEventContact = CalendarEntry.ContactName
    CurrentEventContactEmail = CalendarEntry.ContactEMail
    CurrentEventName = CalendarEntry.ShortDescription

    'CurrentEventName = Replace(CurrentEventName, "'", "''")

    'Replace(CurrentEventName, "'", "\x27")
    Replace(CurrentEventName, "'", "'")

    PopUpWindowTitle = CurrentEventName 'will do for now

    PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &
    Chr(34) & ", " & Chr(34) & CurrentEventDetails & Chr(34) & ", " & Chr(34) &
    CurrentEventContact & Chr(34) & ", " & Chr(34) & CurrentEventContactEmail &
    Chr(34) & ", " & Chr(34) & CurrentDate & Chr(34) & ")"

    strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" &
    CurrentEventName & "</A><BR>"
    End If
    Next

    Return strTemp
    -------------------------
    Sample runtime value of PopupLink:
    "javascript:popUpWindow("Bob's Birthday Party", "Bob made it another year!",
    "Bob", "", "11/3/2003")"

    Sample runtime value of strTemp:
    "<BR><A HREF='#' onClick='javascript:popUpWindow("Bob's Birthday Party",
    "Bob made it another year!", "Bob", "", "11/3/2003")'>Bob's
    Birthday Party</A><BR>"

    The value "Bob's Birthday Party" comes from a database. If I remove the
    apostrophe in the database, then the error does not occur.

    Finally, This is obviously a function call; strTemp is eventually written by
    the calling code to the .text property of a Label control.

    Thanks for taking the time...

    Jeff


    "mikeb" <> wrote in message
    news:e1y%239K%...
    > Jeff S wrote:
    > > In a VB.NET code behind module, I build a string for a link that points

    to a
    > > JavaScript function. The two lines of code below show what is relevant.
    > >
    > > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &

    Chr(34)
    > > & ", " & Chr(34) & CurrentEventDetails & ")"
    > > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
    > > "</A><BR>"
    > >
    > > The problem I have is that when the string variables [PopUpWindowTitle]

    or
    > > [CurrentEventDetails] contain a string with an apostrophe, then the

    server
    > > throws an error "Unterminated String Constant". This error shows up

    prior to
    > > the page rendering in the browser (or as the page is being rendered to

    the
    > > browser - I'm not sure which - but it's before the page shows up in the
    > > browser).
    > >
    > > I tried including JavaScript's "\" escape character - but this does not
    > > solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,

    "'",
    > > "\' "). The server still throws the Unterminated String Constant error.
    > >
    > > What can I do about this? I need to include apostrophes in the

    JavaScript
    > > function call string and VB.NET is ignoring JavaScript's Escape
    > > character -yet honoring JavaScript's syntax rules.
    > >
    > > Thanks in advance.
    > >
    > >

    >
    > Are you really getting the error on the server, not the client?
    >
    > I can see a couple possible problems:
    >
    > 1) in your 2 line sample above, you are missing a Chr(34) term in the
    > expression that builds PopupLink. it seems that the end of that
    > expression should be:
    >
    > CurrentEventDetails & Chr(34) & ")"
    >
    > not:
    >
    > CurrentEventDetails & ")"
    >
    > since you presumably need a closing double quote for the
    > CurrentEventDetails parameter to the client side PopUpWindow() function.
    >
    > I'd be willing to bet that the missing Chr(34) expression is in your
    > code, it's probably just a typo in your post - <soapbox> to which I say,
    > "cut-n-paste is your friend". It's inexcusable to have typos in code
    > snippets that can be pasted from the real code. I mean, if you have a
    > typo in your snippet that is supposed to demonstrate the problem, how
    > are the readers supposed to know what the problem really is? </soapbox>
    >
    > 2) this is what I believe to be your real problem. Instead of:
    >
    > Replace(PopUpWindowTitle, "'","\' ")
    >
    > try using:
    >
    > Replace(PopUpWindowTitle, "'","\x27")
    >
    > or:
    >
    > Replace(PopUpWindowTitle, "'","'")
    >
    >
    > The browser doesn't do JavaScript-style handling of escape sequences -
    > it uses Character Entities to escape special characters. And the
    > browser obviously needs to process the page before it can pass bits of
    > the page off to the JavaScript processor.
    >
    > If you replace apostrophes with "\x27", that string of characters means
    > nothing to the browser, so that's what gets passed in to the JavaScript
    > processor, which sees it as an escape sequence for the apostrophe.
    >
    > If you replace with "'", that string of characters is an 'Character
    > Entity' which the browser translates into an apostrophe before passing
    > the string on to the JavaScript processor.
    >
    > Sorry for the long post.
    > --
    > mikeb
    >
    Jeff S, Nov 5, 2003
    #4
  5. Jeff S

    Jeff S Guest

    Thanks, but Response.Write is not relevant to the original problem. You
    might want to read questions before posting a response, or at least provide
    an explanation of how your response is relevant to the specific question as
    asked (if it's not obvious how it relates).




    "psb" <> wrote in message
    news:OAFHrY%...
    > response.write ("this is a double "" quote")
    >
    > output = this is a double " quote
    >
    >
    >
    >
    > "Jeff S" <> wrote in message
    > news:...
    > > In a VB.NET code behind module, I build a string for a link that points

    to
    > a
    > > JavaScript function. The two lines of code below show what is relevant.
    > >
    > > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &

    > Chr(34)
    > > & ", " & Chr(34) & CurrentEventDetails & ")"
    > > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
    > > "</A><BR>"
    > >
    > > The problem I have is that when the string variables [PopUpWindowTitle]

    or
    > > [CurrentEventDetails] contain a string with an apostrophe, then the

    server
    > > throws an error "Unterminated String Constant". This error shows up

    prior
    > to
    > > the page rendering in the browser (or as the page is being rendered to

    the
    > > browser - I'm not sure which - but it's before the page shows up in the
    > > browser).
    > >
    > > I tried including JavaScript's "\" escape character - but this does not
    > > solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,

    "'",
    > > "\' "). The server still throws the Unterminated String Constant error.
    > >
    > > What can I do about this? I need to include apostrophes in the

    JavaScript
    > > function call string and VB.NET is ignoring JavaScript's Escape
    > > character -yet honoring JavaScript's syntax rules.
    > >
    > > Thanks in advance.
    > >
    > >

    >
    >
    Jeff S, Nov 5, 2003
    #5
  6. Jeff S

    psb Guest

    JUST SAY THANKS YOU BIATCH.

    Dim PopUpLink As String
    Dim PopUpWindowTitle As String = "window's title"
    Dim CurrentEventDetails As String = "currentevent'sdetails"
    Dim strTemp As String
    Dim eventName As String

    PopUpLink = "javascript:popUpWindow('" & PopUpWindowTitle.Replace("'",
    "\'") & "', '" & CurrentEventDetails.Replace("'", "\'") & "')"
    strTemp += "<BR><A HREF=""#"" onClick=""" & PopUpLink & """>" &
    eventName & "</A><BR>"
    Response.Write(strTemp)

    JUST BECAUSE YOU ARE NOT GOOD AT DEBUGGING JAVASCRIPT AND HTML DON'T GET
    HUFFY AT ME.

    THIS WAS EASY!

    "Jeff S" <> wrote in message
    news:%232BIh1%...
    > Thanks, but Response.Write is not relevant to the original problem. You
    > might want to read questions before posting a response, or at least

    provide
    > an explanation of how your response is relevant to the specific question

    as
    > asked (if it's not obvious how it relates).
    >
    >
    >
    >
    > "psb" <> wrote in message
    > news:OAFHrY%...
    > > response.write ("this is a double "" quote")
    > >
    > > output = this is a double " quote
    > >
    > >
    > >
    > >
    > > "Jeff S" <> wrote in message
    > > news:...
    > > > In a VB.NET code behind module, I build a string for a link that

    points
    > to
    > > a
    > > > JavaScript function. The two lines of code below show what is

    relevant.
    > > >
    > > > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &

    > > Chr(34)
    > > > & ", " & Chr(34) & CurrentEventDetails & ")"
    > > > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName

    &
    > > > "</A><BR>"
    > > >
    > > > The problem I have is that when the string variables

    [PopUpWindowTitle]
    > or
    > > > [CurrentEventDetails] contain a string with an apostrophe, then the

    > server
    > > > throws an error "Unterminated String Constant". This error shows up

    > prior
    > > to
    > > > the page rendering in the browser (or as the page is being rendered to

    > the
    > > > browser - I'm not sure which - but it's before the page shows up in

    the
    > > > browser).
    > > >
    > > > I tried including JavaScript's "\" escape character - but this does

    not
    > > > solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,

    > "'",
    > > > "\' "). The server still throws the Unterminated String Constant

    error.
    > > >
    > > > What can I do about this? I need to include apostrophes in the

    > JavaScript
    > > > function call string and VB.NET is ignoring JavaScript's Escape
    > > > character -yet honoring JavaScript's syntax rules.
    > > >
    > > > Thanks in advance.
    > > >
    > > >

    > >
    > >

    >
    >
    psb, Nov 5, 2003
    #6
  7. Jeff S

    Jeff S Guest

    Thanks - you saved me a bunch of time.

    That's big of you to get all offended and STILL take the time to write an
    accurate and understandable response. It works great. I obviously wasn't
    high enough on the HTML/Javascript learning code to see how your original
    response was relevant. Thanks for the boost!




    "psb" <> wrote in message
    news:%23Nuz3E$...
    > JUST SAY THANKS YOU BIATCH.
    >
    > Dim PopUpLink As String
    > Dim PopUpWindowTitle As String = "window's title"
    > Dim CurrentEventDetails As String = "currentevent'sdetails"
    > Dim strTemp As String
    > Dim eventName As String
    >
    > PopUpLink = "javascript:popUpWindow('" & PopUpWindowTitle.Replace("'",
    > "\'") & "', '" & CurrentEventDetails.Replace("'", "\'") & "')"
    > strTemp += "<BR><A HREF=""#"" onClick=""" & PopUpLink & """>" &
    > eventName & "</A><BR>"
    > Response.Write(strTemp)
    >
    > JUST BECAUSE YOU ARE NOT GOOD AT DEBUGGING JAVASCRIPT AND HTML DON'T GET
    > HUFFY AT ME.
    >
    > THIS WAS EASY!
    >
    > "Jeff S" <> wrote in message
    > news:%232BIh1%...
    > > Thanks, but Response.Write is not relevant to the original problem. You
    > > might want to read questions before posting a response, or at least

    > provide
    > > an explanation of how your response is relevant to the specific question

    > as
    > > asked (if it's not obvious how it relates).
    > >
    > >
    > >
    > >
    > > "psb" <> wrote in message
    > > news:OAFHrY%...
    > > > response.write ("this is a double "" quote")
    > > >
    > > > output = this is a double " quote
    > > >
    > > >
    > > >
    > > >
    > > > "Jeff S" <> wrote in message
    > > > news:...
    > > > > In a VB.NET code behind module, I build a string for a link that

    > points
    > > to
    > > > a
    > > > > JavaScript function. The two lines of code below show what is

    > relevant.
    > > > >
    > > > > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &
    > > > Chr(34)
    > > > > & ", " & Chr(34) & CurrentEventDetails & ")"
    > > > > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" &

    EventName
    > &
    > > > > "</A><BR>"
    > > > >
    > > > > The problem I have is that when the string variables

    > [PopUpWindowTitle]
    > > or
    > > > > [CurrentEventDetails] contain a string with an apostrophe, then the

    > > server
    > > > > throws an error "Unterminated String Constant". This error shows up

    > > prior
    > > > to
    > > > > the page rendering in the browser (or as the page is being rendered

    to
    > > the
    > > > > browser - I'm not sure which - but it's before the page shows up in

    > the
    > > > > browser).
    > > > >
    > > > > I tried including JavaScript's "\" escape character - but this does

    > not
    > > > > solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,

    > > "'",
    > > > > "\' "). The server still throws the Unterminated String Constant

    > error.
    > > > >
    > > > > What can I do about this? I need to include apostrophes in the

    > > JavaScript
    > > > > function call string and VB.NET is ignoring JavaScript's Escape
    > > > > character -yet honoring JavaScript's syntax rules.
    > > > >
    > > > > Thanks in advance.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Jeff S, Nov 5, 2003
    #7
  8. Jeff S

    mikeb Guest

    Jeff S wrote:
    > Apparently you saw right through my little ploy to post simpler code to the
    > newsgroup than what I was actually working with (and with the omission/typo
    > you correctly concluded). Your <soapbox> points are well taken...
    >
    > I proceeded to go with your suggestions, but I still get the error. So, I
    > posted the complete section of code below.


    Change this section of code:

    Replace(CurrentEventName, "'", "'")

    PopUpWindowTitle = CurrentEventName 'will do for now

    to:

    PopUpWindowTitle = Replace(CurrentEventName, "'", "'")

    As your code stands now, you're throwing away the results of the
    Replace() call.

    Note that you'll have to something similar for each of your parameters
    to PopUpWindow()

    >
    > Additionally, on closer observation, the sequence that leads to the error
    > message appearing is as follows:
    > 1. From the VS.NET IDE, I press F5 to run the project (with the ASPX page in
    > question set as the start page).
    > 2. The browser window appear, and I see the top part of the ASPX page
    > rendered (e.g., the page header graphics show up).
    > 3. The messagebox appears displaying the "Error: Unterminated string
    > constant" message.
    > 4. At this point, only the top part of the page is rendered, and the message
    > box is displayed in the middle of the browser window. If I click on either
    > Yes or No (the only two buttons on the message box - prompting to debug or
    > not), the page does finish rendering in the browser. The page looks fine,
    > but the links are dead (presumably because the JavaScript function call part
    > of the link is invalid).
    >
    > Here's the code (followed by runtime sample values):
    >
    > For Each CalendarEntry In m_CalendarEntries
    > If CalendarEntry.EventDateTime = DateToGet Then
    >
    > 'Get the for the JS popup window ready
    > CurrentDate = CStr(CalendarEntry.EventDateTime)
    > CurrentEventDetails = CalendarEntry.LongDescription
    > CurrentEventContact = CalendarEntry.ContactName
    > CurrentEventContactEmail = CalendarEntry.ContactEMail
    > CurrentEventName = CalendarEntry.ShortDescription
    >
    > 'CurrentEventName = Replace(CurrentEventName, "'", "''")
    >
    > 'Replace(CurrentEventName, "'", "\x27")
    > Replace(CurrentEventName, "'", "'")
    >
    > PopUpWindowTitle = CurrentEventName 'will do for now
    >
    > PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &
    > Chr(34) & ", " & Chr(34) & CurrentEventDetails & Chr(34) & ", " & Chr(34) &
    > CurrentEventContact & Chr(34) & ", " & Chr(34) & CurrentEventContactEmail &
    > Chr(34) & ", " & Chr(34) & CurrentDate & Chr(34) & ")"
    >
    > strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" &
    > CurrentEventName & "</A><BR>"
    > End If
    > Next
    >
    > Return strTemp
    > -------------------------
    > Sample runtime value of PopupLink:
    > "javascript:popUpWindow("Bob's Birthday Party", "Bob made it another year!",
    > "Bob", "", "11/3/2003")"
    >
    > Sample runtime value of strTemp:
    > "<BR><A HREF='#' onClick='javascript:popUpWindow("Bob's Birthday Party",
    > "Bob made it another year!", "Bob", "", "11/3/2003")'>Bob's
    > Birthday Party</A><BR>"
    >
    > The value "Bob's Birthday Party" comes from a database. If I remove the
    > apostrophe in the database, then the error does not occur.
    >
    > Finally, This is obviously a function call; strTemp is eventually written by
    > the calling code to the .text property of a Label control.
    >
    > Thanks for taking the time...
    >
    > Jeff
    >
    >
    > "mikeb" <> wrote in message
    > news:e1y%239K%...
    >
    >>Jeff S wrote:
    >>
    >>>In a VB.NET code behind module, I build a string for a link that points

    >
    > to a
    >
    >>>JavaScript function. The two lines of code below show what is relevant.
    >>>
    >>>PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &

    >
    > Chr(34)
    >
    >>>& ", " & Chr(34) & CurrentEventDetails & ")"
    >>>strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName &
    >>>"</A><BR>"
    >>>
    >>>The problem I have is that when the string variables [PopUpWindowTitle]

    >
    > or
    >
    >>>[CurrentEventDetails] contain a string with an apostrophe, then the

    >
    > server
    >
    >>>throws an error "Unterminated String Constant". This error shows up

    >
    > prior to
    >
    >>>the page rendering in the browser (or as the page is being rendered to

    >
    > the
    >
    >>>browser - I'm not sure which - but it's before the page shows up in the
    >>>browser).
    >>>
    >>>I tried including JavaScript's "\" escape character - but this does not
    >>>solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,

    >
    > "'",
    >
    >>>"\' "). The server still throws the Unterminated String Constant error.
    >>>
    >>>What can I do about this? I need to include apostrophes in the

    >
    > JavaScript
    >
    >>>function call string and VB.NET is ignoring JavaScript's Escape
    >>>character -yet honoring JavaScript's syntax rules.
    >>>
    >>>Thanks in advance.
    >>>
    >>>

    >>
    >>Are you really getting the error on the server, not the client?
    >>
    >>I can see a couple possible problems:
    >>
    >> 1) in your 2 line sample above, you are missing a Chr(34) term in the
    >>expression that builds PopupLink. it seems that the end of that
    >>expression should be:
    >>
    >> CurrentEventDetails & Chr(34) & ")"
    >>
    >>not:
    >>
    >> CurrentEventDetails & ")"
    >>
    >>since you presumably need a closing double quote for the
    >>CurrentEventDetails parameter to the client side PopUpWindow() function.
    >>
    >>I'd be willing to bet that the missing Chr(34) expression is in your
    >>code, it's probably just a typo in your post - <soapbox> to which I say,
    >> "cut-n-paste is your friend". It's inexcusable to have typos in code
    >>snippets that can be pasted from the real code. I mean, if you have a
    >>typo in your snippet that is supposed to demonstrate the problem, how
    >>are the readers supposed to know what the problem really is? </soapbox>
    >>
    >> 2) this is what I believe to be your real problem. Instead of:
    >>
    >> Replace(PopUpWindowTitle, "'","\' ")
    >>
    >> try using:
    >>
    >> Replace(PopUpWindowTitle, "'","\x27")
    >>
    >> or:
    >>
    >> Replace(PopUpWindowTitle, "'","'")
    >>
    >>
    >>The browser doesn't do JavaScript-style handling of escape sequences -
    >>it uses Character Entities to escape special characters. And the
    >>browser obviously needs to process the page before it can pass bits of
    >>the page off to the JavaScript processor.
    >>
    >>If you replace apostrophes with "\x27", that string of characters means
    >>nothing to the browser, so that's what gets passed in to the JavaScript
    >>processor, which sees it as an escape sequence for the apostrophe.
    >>
    >>If you replace with "'", that string of characters is an 'Character
    >>Entity' which the browser translates into an apostrophe before passing
    >>the string on to the JavaScript processor.
    >>
    >>Sorry for the long post.
    >>--
    >>mikeb
    >>

    >


    --
    mikeb
    mikeb, Nov 6, 2003
    #8
  9. Jeff S

    mikeb Guest

    Jeff S wrote:

    > Thanks - you saved me a bunch of time.
    >
    > That's big of you to get all offended and STILL take the time to write an
    > accurate and understandable response. It works great. I obviously wasn't
    > high enough on the HTML/Javascript learning code to see how your original
    > response was relevant. Thanks for the boost!
    >


    One thing to be careful of, is that with the posted fix you will run
    into the same problem if the text you pull out of the database contains
    a double-quote character. Admittedly, this will be rare (or
    non-existent) compared with the apostrophe, but it might bite you at
    some point.

    >
    >
    >
    > "psb" <> wrote in message
    > news:%23Nuz3E$...
    >
    >>JUST SAY THANKS YOU BIATCH.
    >>
    >> Dim PopUpLink As String
    >> Dim PopUpWindowTitle As String = "window's title"
    >> Dim CurrentEventDetails As String = "currentevent'sdetails"
    >> Dim strTemp As String
    >> Dim eventName As String
    >>
    >> PopUpLink = "javascript:popUpWindow('" & PopUpWindowTitle.Replace("'",
    >>"\'") & "', '" & CurrentEventDetails.Replace("'", "\'") & "')"
    >> strTemp += "<BR><A HREF=""#"" onClick=""" & PopUpLink & """>" &
    >>eventName & "</A><BR>"
    >> Response.Write(strTemp)
    >>
    >>JUST BECAUSE YOU ARE NOT GOOD AT DEBUGGING JAVASCRIPT AND HTML DON'T GET
    >>HUFFY AT ME.
    >>
    >>THIS WAS EASY!
    >>
    >>"Jeff S" <> wrote in message
    >>news:%232BIh1%...
    >>
    >>>Thanks, but Response.Write is not relevant to the original problem. You
    >>>might want to read questions before posting a response, or at least

    >>
    >>provide
    >>
    >>>an explanation of how your response is relevant to the specific question

    >>
    >>as
    >>
    >>>asked (if it's not obvious how it relates).
    >>>
    >>>
    >>>
    >>>
    >>>"psb" <> wrote in message
    >>>news:OAFHrY%...
    >>>
    >>>>response.write ("this is a double "" quote")
    >>>>
    >>>>output = this is a double " quote
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>"Jeff S" <> wrote in message
    >>>>news:...
    >>>>
    >>>>>In a VB.NET code behind module, I build a string for a link that

    >>
    >>points
    >>
    >>>to
    >>>
    >>>>a
    >>>>
    >>>>>JavaScript function. The two lines of code below show what is

    >>
    >>relevant.
    >>
    >>>>>PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &
    >>>>
    >>>>Chr(34)
    >>>>
    >>>>>& ", " & Chr(34) & CurrentEventDetails & ")"
    >>>>>strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" &

    >
    > EventName
    >
    >>&
    >>
    >>>>>"</A><BR>"
    >>>>>
    >>>>>The problem I have is that when the string variables

    >>
    >>[PopUpWindowTitle]
    >>
    >>>or
    >>>
    >>>>>[CurrentEventDetails] contain a string with an apostrophe, then the
    >>>
    >>>server
    >>>
    >>>>>throws an error "Unterminated String Constant". This error shows up
    >>>
    >>>prior
    >>>
    >>>>to
    >>>>
    >>>>>the page rendering in the browser (or as the page is being rendered

    >
    > to
    >
    >>>the
    >>>
    >>>>>browser - I'm not sure which - but it's before the page shows up in

    >>
    >>the
    >>
    >>>>>browser).
    >>>>>
    >>>>>I tried including JavaScript's "\" escape character - but this does

    >>
    >>not
    >>
    >>>>>solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,
    >>>
    >>>"'",
    >>>
    >>>>>"\' "). The server still throws the Unterminated String Constant

    >>
    >>error.
    >>
    >>>>>What can I do about this? I need to include apostrophes in the
    >>>
    >>>JavaScript
    >>>
    >>>>>function call string and VB.NET is ignoring JavaScript's Escape
    >>>>>character -yet honoring JavaScript's syntax rules.
    >>>>>
    >>>>>Thanks in advance.
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>

    >>

    >
    >
    mikeb, Nov 6, 2003
    #9
  10. Jeff S

    keyur shah Guest

    You are initiating char(34) where are u ending it in ur code...
    surprising.... it wont work until u balance parenthesis, quotes..
    blaaaah blaaaah... :)

    Keyur Shah
    Verizon Communications
    732-423-0745

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    keyur shah, Nov 6, 2003
    #10
  11. Jeff S

    psb Guest

    just do a .replace("""","&quot;"). I am not 100% on this solution, although
    I would try this first. one thing I forgot to write in my first post was
    that all things would be easier if all javascript used single quotes and
    all html used double quotes. that is the intended use i believe. very
    rarely will you have to use single quotes in html. same goes with
    javascript; rarely use double quotes inside javascript.

    "mikeb" <> wrote in message
    news:ul4D0x$...
    > Jeff S wrote:
    >
    > > Thanks - you saved me a bunch of time.
    > >
    > > That's big of you to get all offended and STILL take the time to write

    an
    > > accurate and understandable response. It works great. I obviously wasn't
    > > high enough on the HTML/Javascript learning code to see how your

    original
    > > response was relevant. Thanks for the boost!
    > >

    >
    > One thing to be careful of, is that with the posted fix you will run
    > into the same problem if the text you pull out of the database contains
    > a double-quote character. Admittedly, this will be rare (or
    > non-existent) compared with the apostrophe, but it might bite you at
    > some point.
    >
    > >
    > >
    > >
    > > "psb" <> wrote in message
    > > news:%23Nuz3E$...
    > >
    > >>JUST SAY THANKS YOU BIATCH.
    > >>
    > >> Dim PopUpLink As String
    > >> Dim PopUpWindowTitle As String = "window's title"
    > >> Dim CurrentEventDetails As String = "currentevent'sdetails"
    > >> Dim strTemp As String
    > >> Dim eventName As String
    > >>
    > >> PopUpLink = "javascript:popUpWindow('" &

    PopUpWindowTitle.Replace("'",
    > >>"\'") & "', '" & CurrentEventDetails.Replace("'", "\'") & "')"
    > >> strTemp += "<BR><A HREF=""#"" onClick=""" & PopUpLink & """>" &
    > >>eventName & "</A><BR>"
    > >> Response.Write(strTemp)
    > >>
    > >>JUST BECAUSE YOU ARE NOT GOOD AT DEBUGGING JAVASCRIPT AND HTML DON'T GET
    > >>HUFFY AT ME.
    > >>
    > >>THIS WAS EASY!
    > >>
    > >>"Jeff S" <> wrote in message
    > >>news:%232BIh1%...
    > >>
    > >>>Thanks, but Response.Write is not relevant to the original problem. You
    > >>>might want to read questions before posting a response, or at least
    > >>
    > >>provide
    > >>
    > >>>an explanation of how your response is relevant to the specific

    question
    > >>
    > >>as
    > >>
    > >>>asked (if it's not obvious how it relates).
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>"psb" <> wrote in message
    > >>>news:OAFHrY%...
    > >>>
    > >>>>response.write ("this is a double "" quote")
    > >>>>
    > >>>>output = this is a double " quote
    > >>>>
    > >>>>
    > >>>>
    > >>>>
    > >>>>"Jeff S" <> wrote in message
    > >>>>news:...
    > >>>>
    > >>>>>In a VB.NET code behind module, I build a string for a link that
    > >>
    > >>points
    > >>
    > >>>to
    > >>>
    > >>>>a
    > >>>>
    > >>>>>JavaScript function. The two lines of code below show what is
    > >>
    > >>relevant.
    > >>
    > >>>>>PopupLink = "javascript:popUpWindow(" & Chr(34) & PopUpWindowTitle &
    > >>>>
    > >>>>Chr(34)
    > >>>>
    > >>>>>& ", " & Chr(34) & CurrentEventDetails & ")"
    > >>>>>strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" &

    > >
    > > EventName
    > >
    > >>&
    > >>
    > >>>>>"</A><BR>"
    > >>>>>
    > >>>>>The problem I have is that when the string variables
    > >>
    > >>[PopUpWindowTitle]
    > >>
    > >>>or
    > >>>
    > >>>>>[CurrentEventDetails] contain a string with an apostrophe, then the
    > >>>
    > >>>server
    > >>>
    > >>>>>throws an error "Unterminated String Constant". This error shows up
    > >>>
    > >>>prior
    > >>>
    > >>>>to
    > >>>>
    > >>>>>the page rendering in the browser (or as the page is being rendered

    > >
    > > to
    > >
    > >>>the
    > >>>
    > >>>>>browser - I'm not sure which - but it's before the page shows up in
    > >>
    > >>the
    > >>
    > >>>>>browser).
    > >>>>>
    > >>>>>I tried including JavaScript's "\" escape character - but this does
    > >>
    > >>not
    > >>
    > >>>>>solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle,
    > >>>
    > >>>"'",
    > >>>
    > >>>>>"\' "). The server still throws the Unterminated String Constant
    > >>
    > >>error.
    > >>
    > >>>>>What can I do about this? I need to include apostrophes in the
    > >>>
    > >>>JavaScript
    > >>>
    > >>>>>function call string and VB.NET is ignoring JavaScript's Escape
    > >>>>>character -yet honoring JavaScript's syntax rules.
    > >>>>>
    > >>>>>Thanks in advance.
    > >>>>>
    > >>>>>
    > >>>>
    > >>>>
    > >>>
    > >>

    > >
    > >

    >
    psb, Nov 6, 2003
    #11
  12. Jeff S

    mikeb Guest

    psb wrote:
    > just do a .replace("""","&quot;"). I am not 100% on this solution, although
    > I would try this first. one thing I forgot to write in my first post was
    > that all things would be easier if all javascript used single quotes and
    > all html used double quotes. that is the intended use i believe. very
    > rarely will you have to use single quotes in html. same goes with
    > javascript; rarely use double quotes inside javascript.
    >


    However, strings that come out of a database should always be escaped
    such that the data can't cause side effects when sent to the client
    since those strings are usually entered by an end user. If the proper
    escaping isn't done, you leave open the possibility of cross-site
    scripting attacks, or that arbitrary data entered by the user simply
    breaks the script or doesn't display properly.

    So what I would do is write a utility function that did something like this:

    Public Shared Function JScriptEncode(ByRef s As String) As String
    s = Replace(s, "'", "\x27") ' JScript encode apostrophes
    s = Replace(s, """", "\x22") ' JScript encode double-quotes
    s = HttpUtility.HtmlEncode(s) ' encode chars special to HTML
    Return s
    End Function

    Now, there are no characters in the string that will bother the browser,
    and the string can contain double-quotes and/or single-quotes, and the
    browser and the JavaScript processor will not be confused.

    And it doesn't matter what type of quote you use to enclose the string.


    > "mikeb" <> wrote in message
    > news:ul4D0x$...
    >
    >>Jeff S wrote:
    >>
    >>
    >>>Thanks - you saved me a bunch of time.
    >>>
    >>>That's big of you to get all offended and STILL take the time to write

    >
    > an
    >
    >>>accurate and understandable response. It works great. I obviously wasn't
    >>>high enough on the HTML/Javascript learning code to see how your

    >
    > original
    >
    >>>response was relevant. Thanks for the boost!
    >>>

    >>
    >>One thing to be careful of, is that with the posted fix you will run
    >>into the same problem if the text you pull out of the database contains
    >>a double-quote character. Admittedly, this will be rare (or
    >>non-existent) compared with the apostrophe, but it might bite you at
    >>some point.
    >>
    >>
    >>>
    >>>
    >>>"psb" <> wrote in message
    >>>news:%23Nuz3E$...
    >>>
    >>>
    >>>>JUST SAY THANKS YOU BIATCH.
    >>>>
    >>>> Dim PopUpLink As String
    >>>> Dim PopUpWindowTitle As String = "window's title"
    >>>> Dim CurrentEventDetails As String = "currentevent'sdetails"
    >>>> Dim strTemp As String
    >>>> Dim eventName As String
    >>>>
    >>>> PopUpLink = "javascript:popUpWindow('" &

    >
    > PopUpWindowTitle.Replace("'",
    >
    >>>>"\'") & "', '" & CurrentEventDetails.Replace("'", "\'") & "')"
    >>>> strTemp += "<BR><A HREF=""#"" onClick=""" & PopUpLink & """>" &
    >>>>eventName & "</A><BR>"
    >>>> Response.Write(strTemp)
    >>>>
    >>>>JUST BECAUSE YOU ARE NOT GOOD AT DEBUGGING JAVASCRIPT AND HTML DON'T GET
    >>>>HUFFY AT ME.
    >>>>
    >>>>THIS WAS EASY!
    >>>>


    --
    mikeb
    mikeb, Nov 7, 2003
    #12
  13. Jeff S

    Jeff S Guest

    Thanks to both psb and mikeb
    I learned a lot more about Javascript and strings from this one thread than
    I ever expected.




    "mikeb" <> wrote in message
    news:...
    > psb wrote:
    > > just do a .replace("""","&quot;"). I am not 100% on this solution,

    although
    > > I would try this first. one thing I forgot to write in my first post

    was
    > > that all things would be easier if all javascript used single quotes and
    > > all html used double quotes. that is the intended use i believe. very
    > > rarely will you have to use single quotes in html. same goes with
    > > javascript; rarely use double quotes inside javascript.
    > >

    >
    > However, strings that come out of a database should always be escaped
    > such that the data can't cause side effects when sent to the client
    > since those strings are usually entered by an end user. If the proper
    > escaping isn't done, you leave open the possibility of cross-site
    > scripting attacks, or that arbitrary data entered by the user simply
    > breaks the script or doesn't display properly.
    >
    > So what I would do is write a utility function that did something like

    this:
    >
    > Public Shared Function JScriptEncode(ByRef s As String) As String
    > s = Replace(s, "'", "\x27") ' JScript encode apostrophes
    > s = Replace(s, """", "\x22") ' JScript encode double-quotes
    > s = HttpUtility.HtmlEncode(s) ' encode chars special to HTML
    > Return s
    > End Function
    >
    > Now, there are no characters in the string that will bother the browser,
    > and the string can contain double-quotes and/or single-quotes, and the
    > browser and the JavaScript processor will not be confused.
    >
    > And it doesn't matter what type of quote you use to enclose the string.
    >
    >
    > > "mikeb" <> wrote in message
    > > news:ul4D0x$...
    > >
    > >>Jeff S wrote:
    > >>
    > >>
    > >>>Thanks - you saved me a bunch of time.
    > >>>
    > >>>That's big of you to get all offended and STILL take the time to write

    > >
    > > an
    > >
    > >>>accurate and understandable response. It works great. I obviously

    wasn't
    > >>>high enough on the HTML/Javascript learning code to see how your

    > >
    > > original
    > >
    > >>>response was relevant. Thanks for the boost!
    > >>>
    > >>
    > >>One thing to be careful of, is that with the posted fix you will run
    > >>into the same problem if the text you pull out of the database contains
    > >>a double-quote character. Admittedly, this will be rare (or
    > >>non-existent) compared with the apostrophe, but it might bite you at
    > >>some point.
    > >>
    > >>
    > >>>
    > >>>
    > >>>"psb" <> wrote in message
    > >>>news:%23Nuz3E$...
    > >>>
    > >>>
    > >>>>JUST SAY THANKS YOU BIATCH.
    > >>>>
    > >>>> Dim PopUpLink As String
    > >>>> Dim PopUpWindowTitle As String = "window's title"
    > >>>> Dim CurrentEventDetails As String = "currentevent'sdetails"
    > >>>> Dim strTemp As String
    > >>>> Dim eventName As String
    > >>>>
    > >>>> PopUpLink = "javascript:popUpWindow('" &

    > >
    > > PopUpWindowTitle.Replace("'",
    > >
    > >>>>"\'") & "', '" & CurrentEventDetails.Replace("'", "\'") & "')"
    > >>>> strTemp += "<BR><A HREF=""#"" onClick=""" & PopUpLink & """>" &
    > >>>>eventName & "</A><BR>"
    > >>>> Response.Write(strTemp)
    > >>>>
    > >>>>JUST BECAUSE YOU ARE NOT GOOD AT DEBUGGING JAVASCRIPT AND HTML DON'T

    GET
    > >>>>HUFFY AT ME.
    > >>>>
    > >>>>THIS WAS EASY!
    > >>>>

    >
    > --
    > mikeb
    >
    Jeff S, Nov 10, 2003
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    11,684
    Julian Reschke
    Jan 24, 2004
  2. slomo
    Replies:
    5
    Views:
    1,505
    Duncan Booth
    Dec 2, 2007
  3. Sanjeev
    Replies:
    3
    Views:
    1,696
    Andrea Francia
    May 3, 2008
  4. Alden Streeter

    Javascript double quote escape character?

    Alden Streeter, Nov 24, 2003, in forum: ASP General
    Replies:
    1
    Views:
    273
    Alden Streeter
    Nov 26, 2003
  5. Mausam
    Replies:
    4
    Views:
    853
Loading...

Share This Page