javascripthtmlescaping

escape & in url


How can I escape & (ampersand) in url with the correct replacement in the jQuery function. I have tried

.replace("/&/g", "&")  
.replace("/&/g", "%26") 
.replace("/&/g", "\&") 

But nothing is working.

If anyone has idea please share with me.

Thanks


Solution

  • If you need to just escape a URL, I recommend using a JavaScript function such as encodeURIComponent.