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.

Steve B
  • 36,818
  • 21
  • 101
  • 174
lior r
  • 2,220
  • 7
  • 43
  • 80
  • Can you add more code? Like how are you handling the ajax response and generating it on server? – Esailija Jul 30 '12 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 Jul 30 '12 at 10:27
  • Did you tried replacing double quote with some other character ?? – yogi Jul 30 '12 at 10:27
  • What is the server side language? – xdazz Jul 30 '12 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 Jul 30 '12 at 10:28
  • What language are you using to return the data from the database? Could you not clean this result on the server side before passing it to the clientside? – StevenMcD Jul 30 '12 at 10:31
  • value="'+ucontent+'" is javascript code ... im getting the value directly from the inputbox (document.getElementById('valuebox') – lior r Jul 30 '12 at 11:16

3 Answers3

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
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.

code-jaff
  • 9,230
  • 4
  • 35
  • 56
0

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

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78
  • i cant add it because it is the user input ... im taking the user input and transfering it to another input on the form. I want to use something like PHP addslashes ... – lior r Jul 30 '12 at 11:39
  • @liorr If you're processing it at all, you can certainly add it :) So when you are "taking the user input and transferring it", you can mess with the user input all you like. In your Javascript, you stick the user input in a variable, and then use a `replace` or equivalent. – Nick Jul 30 '12 at 11:41