最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Escaping javascript variable double quotes - Stack Overflow

programmeradmin0浏览0评论

I'm using Ajax calls to get some variables data from the DB. some of my data stored on the database contains double quotes (").

when I'm trying to display the variable :

value="'+ucontent+'"

the string gets cut in the middle (of course)

I have tried using escape() but im getting a non readable result - something with %4%2 etc...

how can i escape the double quotes in the variable and still keep a readable string... BTW - I'm using UTF8 characters.

I'm using Ajax calls to get some variables data from the DB. some of my data stored on the database contains double quotes (").

when I'm trying to display the variable :

value="'+ucontent+'"

the string gets cut in the middle (of course)

I have tried using escape() but im getting a non readable result - something with %4%2 etc...

how can i escape the double quotes in the variable and still keep a readable string... BTW - I'm using UTF8 characters.

Share Improve this question edited Jul 30, 2012 at 10:24 Steve B 37.8k23 gold badges109 silver badges184 bronze badges asked Jul 30, 2012 at 10:23 lior rlior r 2,3007 gold badges44 silver badges82 bronze badges 7
  • Can you add more code? Like how are you handling the ajax response and generating it on server? – Esailija Commented Jul 30, 2012 at 10:26
  • The quotes should already be JavaScript data, so it is unclear what the problem is. Where are you trying to display the variable? Is value an HTML attribute that you are going to use with innerHTML? – Quentin Commented Jul 30, 2012 at 10:27
  • Did you tried replacing double quote with some other character ?? – yogi Commented Jul 30, 2012 at 10:27
  • What is the server side language? – xdazz Commented Jul 30, 2012 at 10:27
  • you are trying to display value when? After ajax call? where does data came from? how do you try to display data? – s.webbandit Commented Jul 30, 2012 at 10:28
 |  Show 2 more ments

3 Answers 3

Reset to default 2
decodeURIComponent() 

might be helpful

what escape actually does is replace some characters with a hexadecimal escape sequence. That is the reason why you are getting unreadable string like %4%2.

Depends on what language in server side you are using.

If it is php, then use json_encode to encode the response string.

If it is ruby(rails), then use escape_javascript to escape the response string.

You can just use \" if you don't use an encoder. See this.

发布评论

评论列表(0)

  1. 暂无评论