Tuesday, June 13, 2017

Working with JavaScript with C# - Single Quote Issue Fixed

Hi Guys,

After some busy days, Here I come again with a small problem I had and of course with the solution too.

Sometimes we have to build JavaScript within our code to inject it to the page. A declared variable for a string in JavaScript can't have a single quote because it gives an error at the end when we need it. This mostly happens with multilingual websites such as in french they use single quote a lot.

Look at the below c# method that can replace the single quote with JS friendly string.


In the same class we can use this as ;


It's simple. Next let's see how we use this to build a XML from C# and store it in JS Local Storage.

Happy coding...

No comments:

Post a Comment