user944513 user944513 - 7 months ago 11
Javascript Question

How to make url in jQuery using query parameter?

I am trying to use this plugin: https://github.com/tombonner/jurlp.

I want to make url using parameter. Can we make using this plugin?

Here is my code

// 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

I have two cases


  • First In url I have already "?" and add param it give expected result using "&"

  • second if url does not contain "?" then it add param with "?" and "&"



Can we use this plugin to make url?

Any update ?

Answer

You can parse the original URL using $.jurlp

var url = $.jurlp(a);

Then you can update the query parameters with the .query() method.

url.query(abc);

Then see the new URL

console.log(url.href);

DEMO

Comments