How to double encodeURIComponent in javascript?

别等时光非礼了梦想. 提交于 2020-06-01 03:11:09

问题


In my web app, I use a mailto link to open the outlook 2007. I also insert a url into the body. The problem is I need the whole url to be a hyperlink. If there is any spaces, then the hyperlink breaks, or even if there is any special characters like if it ends with a close parenthesizes the character wont be included in the hyperlink, so the link breaks.

What I tried was using encodeURIComponent on the link, which url encodes it, but the issue is that in outlook, it will automatically decode it back to normal which then breaks the link. What I need is a way to double encode it.

Basically instead of doing

" " -> "%20" (<-- encodeURIComponent)

I need

" " -> "%2520"

So that in outlook, the %25 gets decoded to %, which when combines with the 20 so I get %20 keeping the link encoded once and not broken. This is what would work for me, but I just don't know how to do this function.

Does anyone know how I can do this double encode?

Thanks


回答1:


Encode

var encoded=escape(encodeURIComponent(' '));

Decode

 var decoded=decodeURIComponent(unescape(encoded));


来源:https://stackoverflow.com/questions/18407078/how-to-double-encodeuricomponent-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!