Single quote in string from data bound control breaks window.open script 
Author Message
 Single quote in string from data bound control breaks window.open script

A text string containing an apostraphe (single quote) creates a javascript
error.

The text string comes from a VS.NET databound control pulling data from the
database, so I can't get at it directly to use various escape options.

Here is the function:
 function openPrint(strPrintID, strDesc){ OpenWin =
this.open("Print.aspx?strPrintID=" + strPrintID + "&strDesc=" + strDesc +
"","popup","toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,w
idth=350,height=375"); }

Here is the call:
<A HREF='javascript:openPrint("<%# DataBinder.Eval(Container,
"DataItem.ItemID") %>", "<%# DataBinder.Eval(Container, "DataItem.ItemName")
%>")';>...

The databound info: # DataBinder.Eval(Container, "DataItem.ItemName") %>
returns the string with the apostraphe the JavaScrip sees: It's Yorkie in a
Bag!

So the call looks like this to JavaScript:
javascript:openPrint("HP-105", "It's Yorkie in a Bag!")

However, the single quote causes it to truncate like this:
javascript:openPrint("HP-105", "It

Hense the error: unterminated string constant.

If I reverse the ' and ", then JavaScript sees the entire:
javascript:openPrint('HP-105', 'It's Yorkie in a Bag!')

But now the single quote creates the error: expected ')'

I've even tried to following to replace the ' with /', but it still breaks
at the single quote.

<A HREF="javascript: var strItemID = new String('<%#
DataBinder.Eval(Container, "DataItem.ItemID") %>'); var strDesc = new
String('<%# DataBinder.Eval(Container, "DataItem.ItemName") %>'); strDesc =
strDesc.replace(',/'); openPrint(strItemID, strDesc)";>
 <IMG SRC='..\prints/<%# DataBinder.Eval(Container, "DataItem.ItemID")
%>t.jpg' BORDER="0"></A>

In the database record, if I use %27 to represent the apostraphe, the script
no longer breaks, but the web page displays the %27 instead of the
apostraphe.

I would appreciate any suggestions.
Gary



Sun, 09 Jan 2005 05:07:01 GMT  
 Single quote in string from data bound control breaks window.open script


Quote:
> ...I've even tried to following to replace the ' with /', but it still

breaks at the single quote...

Have you tried a backslash rather than a forward slash, i.e.: \'

Simon.



Sun, 09 Jan 2005 16:15:55 GMT  
 Single quote in string from data bound control breaks window.open script
What worked was using a replace function inside the databound binder code:

Replace(binder code),"'","\'")

Here is the revised code:

<A HREF="javascript:openPrint('<%# DataBinder.Eval(Container,
"DataItem.ItemID") %>', '<%# Replace(DataBinder.Eval(Container,
"DataItem.ItemName"),"'","\'") %>')";>


Quote:
> A text string containing an apostraphe (single quote) creates a javascript
> error.

> The text string comes from a VS.NET databound control pulling data from
the
> database, so I can't get at it directly to use various escape options.

> Here is the function:
>  function openPrint(strPrintID, strDesc){ OpenWin =
> this.open("Print.aspx?strPrintID=" + strPrintID + "&strDesc=" + strDesc +

"","popup","toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,w
Quote:
> idth=350,height=375"); }

> Here is the call:
> <A HREF='javascript:openPrint("<%# DataBinder.Eval(Container,
> "DataItem.ItemID") %>", "<%# DataBinder.Eval(Container,

"DataItem.ItemName")
Quote:
> %>")';>...

> The databound info: # DataBinder.Eval(Container, "DataItem.ItemName") %>
> returns the string with the apostraphe the JavaScrip sees: It's Yorkie in
a
> Bag!

> So the call looks like this to JavaScript:
> javascript:openPrint("HP-105", "It's Yorkie in a Bag!")

> However, the single quote causes it to truncate like this:
> javascript:openPrint("HP-105", "It

> Hense the error: unterminated string constant.

> If I reverse the ' and ", then JavaScript sees the entire:
> javascript:openPrint('HP-105', 'It's Yorkie in a Bag!')

> But now the single quote creates the error: expected ')'

> I've even tried to following to replace the ' with /', but it still breaks
> at the single quote.

> <A HREF="javascript: var strItemID = new String('<%#
> DataBinder.Eval(Container, "DataItem.ItemID") %>'); var strDesc = new
> String('<%# DataBinder.Eval(Container, "DataItem.ItemName") %>'); strDesc
=
> strDesc.replace(',/'); openPrint(strItemID, strDesc)";>
>  <IMG SRC='..\prints/<%# DataBinder.Eval(Container, "DataItem.ItemID")
> %>t.jpg' BORDER="0"></A>

> In the database record, if I use %27 to represent the apostraphe, the
script
> no longer breaks, but the web page displays the %27 instead of the
> apostraphe.

> I would appreciate any suggestions.
> Gary



Mon, 10 Jan 2005 04:51:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. single quote in data creates problem while inserting

2. INSERTing data with single quotes

3. SQL and input data containing a single quote

4. string with single and double quote

5. strings with single quotes

6. Single quotes in string

7. Single Quotes in SQL string

8. Single quote problem in TEXTAREA control

9. Single quotes in SQL string?

10. WMI and Single Quote (String Literal)

11. single quote value not string delimeter

12. Double Quotes, Then Single Quotes, what comes next?

 

 
Powered by phpBB® Forum Software