0

I am getting a value from the server and directly assigning it to a field using javascript. Below is the code.

document.getElementById('myid')='<%= myServerValue%>';

But the issue is when myServerValue contains single quote javascript functionality collapsed. Since it is like document.getElementById('myid')='myvalue'isx'; [there is a single quote]

Please tell me a solution. Using double quotes too will not be a good option, since myServerValue could contain double quotes also...

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
Chathura
  • 11
  • 3

3 Answers3

0

try HttpUtility.JavaScriptStringEncode

hazimdikenli
  • 5,709
  • 8
  • 37
  • 67
0

You need to encode your characters at the server and somehow decode it in the javascript code. There are a few possibilities to achieve that, you can use UTF8 like in this demo The escape and unescape javascript methods are obsolete but you can try String.fromCharCode.

lastboy
  • 566
  • 4
  • 12
0

This no the most clean solution, but it will work just as it is.

document.getElementById('myid')="<%= myServerValue%>";
Oleg Belousov
  • 9,981
  • 14
  • 72
  • 127