2016-02-15 168 views
1

我通过浏览器自动替换为“+”或“%20”来传递变量真正的url和空格。重定向页面之后,我想正确拆分这个词。用空格替换特殊字符

例如: - http://localhost:8008/Login.aspx?stid=Are你快乐

后负荷登录页面这个自动转换,

http://localhost:8008/Login.aspx?stid=Are+You+Happy

http://localhost:8008/Login.aspx?stid=Are%20You%20Happy

如何加载后得到这个STID变量该页面,

你快乐吗

有没有办法做到这一点使用JavaScript或jQuery?

,我想同样的结果,当我把我的参数,

http://localhost:8008/Login.aspx?stid=Are+You+Happy

我想,还要以下的结果。

为+你+快乐

可这两个功能的JavaScript或jQuery的呢?

回答

2

这应该做的伎俩

var url = "http://localhost:8008/Login.aspx?stid=Are%20You%20Happy", 
 
    decoded_url = decodeURIComponent(url.replace(/\+/g, " ")); 
 
    
 
alert(decoded_url.split('stid=')[1]);

+0

jQuery真的需要这个吗? – SlashmanX

+0

不,完全没有。我从代码片段中删除了jquery。 – Timo

4
//decode function 
function decode(encodedStr) { 
    return decodeURIComponent(encodedStr.replace(/\+/g, " ")); 
} 

//encode function 
function encode(unencoded) { 
    return encodeURIComponent(unencoded).replace(/'/g,"%27").replace(/"/g,"%22"); 
} 


您还可以使用 JQuery.params()

var myObject = { 
    stid: 'You are happy', 
    b: [ 1, 2, 3 ], 
    ....... 
}; 
var recursiveEncoded = $.param(myObject); 
var recursiveDecoded = decodeURIComponent($.param(myObject)); 

alert(recursiveEncoded); 
alert(recursiveDecoded);