如何使用查询参数在jQuery中创建url?

时间:2016-04-08 00:43:49

标签: javascript jquery

我正在尝试使用此插件:https://github.com/tombonner/jurlp

我想使用参数制作网址。我们可以使用这个插件吗?

这是我的代码

// first case
var a = 'http://abc/deappsl/index.html?word=abc';
var abc = {
  test:"lo"
}

// second case
var a = 'http://abc/deappsl/index.html';
var abc = {
  test:"lo",
  p:'er'
}

// expected output
//first
//http://abc/deappsl/index.html?word=abc&test=lo

//second
//http://abc/deappsl/index.html?test=lo&p=er

http://plnkr.co/edit/IEnUPyGAgCCCPeII0xh3?p=preview

我有两个案例

  • 首先在网址中我已经“?”并添加参数,使用“&”
  • 给出预期结果
  • 第二,如果网址不包含“?”那么它用“?”添加参数和“&”

我们可以使用此插件制作网址吗?

任何更新?

1 个答案:

答案 0 :(得分:0)

您可以使用$.jurlp

解析原始网址
var url = $.jurlp(a);

然后,您可以使用.query()方法更新查询参数。

url.query(abc);

然后查看新网址

console.log(url.href);

DEMO