分割字符串并使用javascript获取网址值的最佳方法

Jav*_*eek 0 javascript jquery

我有字符串,如下所示

?type=english&time=today
Run Code Online (Sandbox Code Playgroud)

我想获得类型和时间的值,并且如下所示

var str="?type=english&time=today";

var spt =str.split("&");
var typeval =spt[0].split("=")[1];
var timeval =spt[1].split("=")[1];

document.write(" type :"+typeval+" time : "+timeval);
Run Code Online (Sandbox Code Playgroud)

使用javascript获取值的有效方法是什么?

Mat*_*all 5

使用jQuery BBQ的 $.deparam功能.

var str='type=english&time=today',
    obj = $.deparam(str),
    typeval = obj.type, // 'english'
    timeval = obj.time; // 'today'
Run Code Online (Sandbox Code Playgroud)

它适用于各种奇特的URL编码数据结构(参见链接的示例).