McLaren
McLaren

Reputation: 776

Removing parameters from url using jQuery or JavaScript

Hello stackoverflow community. I need ideas and help. Is it possible to remove that pagination from link? for example: http://example.com/comp-filter/page/2/ So i need to remove that page/2/ and i do not know how, because that number can be from 1 to eternity. So what could i do to remove it using jquery or javascript. Help please? For replacing parameters i was using:

function setGetParameter(paramName, paramValue)
{
    var url = window.location.href;
    if (url.indexOf(paramName + "=") >= 0)
    {
        var prefix = url.substring(0, url.indexOf(paramName));
        var suffix = url.substring(url.indexOf(paramName));
        suffix = suffix.substring(suffix.indexOf("=") + 1);
        suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
        url = prefix + paramName + "=" + paramValue + suffix;
    }
    else
    {
    if (url.indexOf("?") < 0)
        url += "?" + paramName + "=" + paramValue;
    else
        url += "&" + paramName + "=" + paramValue;
    }
    return url;
}

But chaning this code wont help...

Upvotes: 1

Views: 133

Answers (1)

Alexander Mikhalchenko
Alexander Mikhalchenko

Reputation: 4565

HTML5's history api is what you're looking for.

What you want to do is replaceState.

history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.

So, your code would be smth like:

history.replaceState(data, document.title, newUrl);

For actual replacement you should try regular expressions:

oldUrl.replace(/page\/\d+\/?/, '');

Best regards, Alexander

Upvotes: 1

Related Questions