Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

apostrophe in var

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
0
0
US

I'm getting a value from a label on a webform and passing it to another form using the code below.

var value = '<%= l10.Text %>';

the problem is when there is an apostrophe within the text. since i never will know what text the label will have, how can I ignore the apostrophe within the entire label.

Any help would be appreciated
 
Hi

This is not JavaScript question. Even if happens that currently you want to output the value inside JavaScript code, you better make a habit of escaping/encoding all values, always. Regardless where they gets inserted.

And just because right now you have problem with a single quote, later you may have a value which ends in backslash ( \ ). So better not concentrate only on that single quote.

As your code looks like ASP to me, I would try [tt]Json.Encode()[/tt] :
Code:
var value = <%= Json.Encode(l10.Text) %>;
Note the lack of surrounding single quotes - [tt]Json.Encode()[/tt] should add those string delimiters too.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top