Decoding URL parameters with JavaScript

后端 未结 5 710
离开以前
离开以前 2020-11-27 19:22

This should be a simple task, but I can\'t seem to find a solution.

I have a basic string that is being passed through as a query string parameter like this one:

5条回答
  •  天命终不由人
    2020-11-27 19:48

    I created my own string methods to support the needed encoding/decoding. These methods will handle the + encoding and decoding properly, allowing you to have plusses (+) in your string and still have the original spaces be encoded as +'s.

    String.prototype.plusEncode = function() {
        return encodeURIComponent(this).replace(/\%20/gm,"+");
    }
    
    String.prototype.plusDecode = function() {
        return decodeURIComponent(this.replace(/\+/gm,"%20"));
    }
    

提交回复
热议问题