How to know if a URL is decoded/encoded?

后端 未结 5 881
后悔当初
后悔当初 2021-02-05 04:37

I am using Javascript method decodeURIComponent to decode an encoded URL. Now I am having an issue, that sometimes the URL is get encoded twice during redirection b

5条回答
  •  忘了有多久
    2021-02-05 05:14

    There is really simple and fast and trusted way to know if a URL string is encoded or Not.

    For Example: decodeURIComponent(decodeURIComponent(encodeURIComponent("SWQgPSA0NjI%3D"))) <> "SWQgPSA0NjI=" ----> Not equal to orginal value then Already encoded

    just this code:

      var encValue = encodeURIComponent(Value);
        try {
             if (decodeURIComponent(decodeURIComponent(encValue)) === Value) {
              //not encodec yet...so return encoded of val
              return encValue;
             }
           } catch (err) {
               //not encodec yet...so return encoded of val
               return encValue;
           }
    
     return Value  //same value returned
    
                            
    

提交回复
热议问题