[SalesForce] How to handle apostrophe, single quote and double quote at the same time

I was trying to include a field value in windows.location of JavaScript which have combination of apostrophe, single quote and double quote. Is there any way I can handle this string combinations?

Best Answer

Use JSENCODE to get it to proper JavaScript escaping, and then encodeURIComponent to make sure the URL is correct.

Example Code:

var someonesName = "{!JSENCODE(Contact.Name)}";
var someUrl = "https://.../?name="+encodeURIComponent(someonesName);