代码之家  ›  专栏  ›  技术社区  ›  at.

javascript url解码功能

  •  153
  • at.  · 技术社区  · 14 年前

    什么是最好的javascript URL解码工具?编码也会很好,并且与jquery一起工作是一个额外的好处。

    8 回复  |  直到 6 年前
        1
  •  215
  •   TJL Geoff    11 年前
        2
  •  231
  •   Matt user129975    13 年前

    这是一个完整的函数(取自 PHPJS ):

    function urldecode(str) {
       return decodeURIComponent((str+'').replace(/\+/g, '%20'));
    }
    
        3
  •  9
  •   Etienne Dupuis    8 年前
    decodeURIComponent(mystring);
    

    您可以使用下面的代码获取传递的参数:

    //parse URL to get values: var i = getUrlVars()["i"];
    function getUrlVars() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    

    或者这一行得到参数:

    location.search.split("your_parameter=")[1]
    
        4
  •  7
  •   nithinreddy    12 年前

    用这个

    unescape(str);
    

    我不是一个伟大的JS程序员,尝试了所有,这工作太棒了!

        5
  •  3
  •   Dennis Hackethal    12 年前
    //How decodeURIComponent Works
    
    function proURIDecoder(val)
    {
      val=val.replace(/\+/g, '%20');
      var str=val.split("%");
      var cval=str[0];
      for (var i=1;i<str.length;i++)
      {
        cval+=String.fromCharCode(parseInt(str[i].substring(0,2),16))+str[i].substring(2);
      }
    
      return cval;
    }
    
    document.write(proURIDecoder(window.location.href));
    
        6
  •  2
  •   Brent Self    10 年前

    如果您负责使用urlencode对php中的数据进行编码,php的rawurlencode可以与javascript的解码组件一起工作,而不需要替换+字符。

        7
  •  1
  •   user3572058    9 年前

    我用的是:

    在JavaScript中:

    var url = "http://www.mynewsfeed.com/articles/index.php?id=17";
    var encoded_url = encodeURIComponent(url);
    
    var decoded_url = decodeURIComponent(encoded_url);
    

    在PHP中:

    $url = "http://www.mynewsfeed.com/articles/index.php?id=17";
    $encoded_url = url_encode(url);
    
    $decoded_url = url_decode($encoded_url);
    

    您也可以在这里在线尝试: http://www.mynewsfeed.x10.mx/articles/index.php?id=17

        8
  •  0
  •   CertainPerformance    6 年前

    var uri = "my test.asp?name=ståle&car=saab";
    console.log(encodeURI(uri));