0

I want to split and join two type of url. For example
Url 1 :
http://localhost/site/index.php?route=product/category&path=20&sort=p.price&order=ASC&order=DESC
Url 2 :
http://localhost/site/index.php?route=product/category&path=20&limit=8

<input type="hidden" class="sort" value="http://localhost/site/index.php?route=product/category&path=20&sort=p.price&order=ASC&order=DESC" />

<input type="hidden" class="limit" value="http://localhost/site/index.php?route=product/category&path=20&limit=8" />

I'd like to join the query strings but remove duplicates.

I'm looking for this result at last

http://localhost/site/index.php?route=product/category&path=20&sort=p.price&order=ASC&order=DESC&limit=8
nicael
  • 18,550
  • 13
  • 57
  • 90
MAHAR
  • 5
  • 1
  • 6
  • 1
    [this question](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) should give you what you need to split the GET parameters. then, all you have to do is aggregate them in a dictionary and rebuild the URL off that dictionary. – Laur Ivan Jul 30 '16 at 13:40

2 Answers2

1
var getUrlParameter = function getUrlParameter(sParam, url) {
    var sPageURL = decodeURIComponent(url),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

Now read individual parametrs by

var order = getUrlParameter('order', 'http://localhost/site/index.php?route=product/category&path=20&sort=p.price&order=ASC&order=DESC');
var limit = getUrlParameter('limit', 'http://localhost/site/index.php?route=product/category&path=20&limit=8');

and make a new url by using the parameters.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
  • Hay working but i don't want to write `http://localhost/site/index.php?route=product/category` manually because i want to add this function dynamically. – MAHAR Jul 30 '16 at 14:16
0

You could go with getting the query parameters in an array and de-duplicating them.

var url1 = "http://localhost/site/index.php?route=product/category&path=20&sort=p.price&order=ASC&order=DESC";
var url2 = "http://localhost/site/index.php?route=product/category&path=20&limit=8";
var url  = (url1.split`?`[1]+"&"+url2.split`?`[1]);
var result = url1.split`?`[0]+"?"+Array.from(new Set(url.split`&`)).join`&`;
console.log(result)

Note that you're left with order=ASC and order=DESC, of which only the last is processed. But looks like that's what you want...

For older browsers:

var url1 = "http://localhost/site/index.php?route=product/category&path=20&sort=p.price&order=ASC&order=DESC";
var url2 = "http://localhost/site/index.php?route=product/category&path=20&limit=8";
var url  = (url1.split('?')[1]+"&"+url2.split('?')[1]);
var result = url1.split('?')[0]+"?"+url.split('&').filter(function(x,i){
  return url.split('&').indexOf(x) == i;
}).join('&');
console.log(result)
Community
  • 1
  • 1
nicael
  • 18,550
  • 13
  • 57
  • 90
  • @MAHAR So you don't want this way? **Why** don't you want this way? – nicael Jul 30 '16 at 14:01
  • The problem is on my site script because i want to dynamically add this using ajax when i add this my whole site's javascript function stop working . But `atulquest93` answer working. So need other easy and short way like your answer. – MAHAR Jul 30 '16 at 14:13
  • @MAHAR Do you use `url` variable elsewhere? – nicael Jul 30 '16 at 14:40
  • @MAHAR Added a snippet for older browsers. – nicael Jul 30 '16 at 17:35