-2

I have three functions to handle between url parameters but its not good enough , i mean its little complicated , and im looking for simple functions to use between all those three .

function getUrlParameter(sParam) {  // this function to get single parameter
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}


function replaceUrlParam(paramName, paramValue) {  // this to replace parameters value by other value
    var currentUrl = window.location.href;
    var pattern = new RegExp('(' + paramName + '=).*?(&|$)')
    var newUrl = currentUrl.replace(pattern, '$1' + paramValue + '$2');
    if (newUrl == currentUrl && newUrl.indexOf(paramName + '=' + paramValue) == -1) {
        newUrl = newUrl + (newUrl.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue
    }
    window.history.pushState('', document.title, newUrl);
    return newUrl;
}



function getUrlParameters() { // this to get all parameters 
    var sParam = 'p'; var viewParam = 'v'; var lanPram = 'l'; var ukomPram = 'uk'; var catPram = 'ca'; var ucatPram = 'uc';

    l = getUrlParameter(lanPram);
    ukmun = getUrlParameter(ukomPram);
    kat = getUrlParameter(catPram);
    uKat = getUrlParameter(ucatPram);
    pagenumber = getUrlParameter(sParam);
    viewIndex = getUrlParameter(viewParam);
    if (l || l == 0) { l = l; } else { l = replaceUrlParam(lanPram, 0); }
    if (ukmun) { ukmun = ukmun; } else { ukmun = replaceUrlParam(ukomPram, 0); }
    if (kat) { kat = kat; } else { kat = replaceUrlParam(catPram, 0); }
    if (uKat) { uKat = uKat; } else { uKat = replaceUrlParam(ucatPram, 0); }
    if (viewIndex) { viewIndex = viewIndex; } else { viewIndex = 'l'; }
    if (pagenumber) { pagenumber = pagenumber; } else { pagenumber = 1; }
}

this works but it calls many the function replaceUrlParam and the function getUrlParameter And the big thing which i dont like is when the page is loaded with those urls then if i want go back to previous page it goes back by every parameter

ex: if have this url myfile.php?p=1&v=l&l=0&uk=0&ca=0&uc=0

then if i click previous to go previous page in browser i go back by one parameter like that

myfile.php?p=1&v=l&l=0&uk=0&ca=0 then to go previous again it stays in same page with next param like that myfile.php?p=1&v=l&l=0&uk=0 and so on till the parameters are gone .

Is there an easy work around those 3 functions to make it easier and simple ?

Thanks.

Ozan
  • 3,709
  • 2
  • 18
  • 23
Scooter Daraf
  • 525
  • 7
  • 23
  • might be a duplicate of http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter – deltree Aug 10 '16 at 18:22
  • @deltree no sorry its totally different . look other answer what he asked . dont just make duplicate without reading other answer . i guess you just read the tittle – Scooter Daraf Aug 10 '16 at 18:23

1 Answers1

-2

First, it seems really inefficient to go through the entire query string every time you get a var. I would suggest parsing the entire query string into an object at once, like this:

function getParameters() {
    // Get just the query string, and split it on &
    var param_arr = window.location.search.substring(1).split('&'),
        param_obj = {};

    // Iterate through results
    for (var i = 0; i < param_arr.length; i++) {
        var param = param_arr[i].split('=');

        if (param === param_arr[i])
            // If this did not have a value, it goes into the object as key = true
            param_obj[param] = true;
        else
            // If this did have a value, we enter it into the object as key = value
            param_obj[param[0]] = param[1];
    }

    return param_obj;
};

Now, getParameters will return an object that looks like this for example:

{
    p: 1,
    v: 1,
    uk: 0
}

As for defaults, the easy way would be to create an object that looks like that, with the default values, and then merge them together. There are lots of libraries that can do this a bit more elegantly and foolproof, but here's a simple example:

function mergeParams(defaults, params) {
    var obj = {};

    for (var i in defaults) {
        if (i in params)
            obj[i] = params[i];
        else
            obj[i] = defaults[i];
    }

    return obj;
}

So you would do:

var defaults = {
    i: 0,
    v: 1
};

var params = getParameters();
params = mergeParams(defaults, params);

All of this is merely a start. There's other possible considerations, depending on your needs, such as parsing the query string encoding. I would highly recommend looking into a library that can do this for you. URI.js could be a good fit: http://medialize.github.io/URI.js/

Chazzu
  • 840
  • 7
  • 5
  • how to see if parameter is defined if not define as i done with replaceparameter ? – Scooter Daraf Aug 10 '16 at 18:40
  • Since this merges the defaults with the parameters in the URL, we get the default values without ever changing the URL. This means you won't have to hit 'back' repeatedly. In fact, the user doesn't even see the default parameters show up in the URL. – Chazzu Aug 10 '16 at 18:53
  • how do i merge one parameter forexample ? can you give example pls – Scooter Daraf Aug 10 '16 at 19:22
  • you have defaults variable with two params , so what if i want add one param or update one parameter pls ? can you exaplin with example pls – Scooter Daraf Aug 10 '16 at 19:23