How to encode periods for URLs in Javascript?

后端 未结 8 1993
耶瑟儿~
耶瑟儿~ 2020-12-15 15:29

The SO post below is comprehensive, but all three methods described fail to encode for periods.

Post: Encode URL in JavaScript?

For instance, if I run the th

8条回答
  •  心在旅途
    2020-12-15 16:14

    I had this same problem where my .htaccess was breaking input values with . Since I did not want to change what the .htaccess was doing I used this to fix it:

    var val="foo.bar";
    var safevalue=encodeURIComponent(val).replace(/\./g, '%2E');
    

    this does all the standard encoding then replaces . with there ascii equivalent %2E. PHP automatically converts back to . in the $_REQUEST value but the .htaccess doesn't see it as a period so things are all good.

提交回复
热议问题