How to Remove the Querystring and Get Only the Url

How can I delete a query string parameter in JavaScript?

"[&;]?" + parameter + "=[^&;]+"

Seems dangerous because it parameter ‘bar’ would match:

?a=b&foobar=c

Also, it would fail if parameter contained any characters that are special in RegExp, such as ‘.’. And it's not a global regex, so it would only remove one instance of the parameter.

I wouldn't use a simple RegExp for this, I'd parse the parameters in and lose the ones you don't want.

function removeURLParameter(url, parameter) {
//prefer to use l.search if you have a location/link object
var urlparts = url.split('?');
if (urlparts.length >= 2) {

var prefix = encodeURIComponent(parameter) + '=';
var pars = urlparts[1].split(/[&;]/g);

//reverse iteration as may be destructive
for (var i = pars.length; i-- > 0;) {
//idiom for string.startsWith
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
pars.splice(i, 1);
}
}

return urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '');
}
return url;
}

Remove querystring from URL

An easy way to get this is:

function getPathFromUrl(url) {
return url.split("?")[0];
}

For those who also wish to remove the hash (not part of the original question) when no querystring exists, that requires a little bit more:

function stripQueryStringAndHashFromPath(url) {
return url.split("?")[0].split("#")[0];
}

EDIT

@caub (originally @crl) suggested a simpler combo that works for both query string and hash (though it uses RegExp, in case anyone has a problem with that):

function getPathFromUrl(url) {
return url.split(/[?#]/)[0];
}

Remove a Particular Query string from URL

Little bit of more search and then you can end up here:

 var url = "www.foo.com/test?name=kevin&gender=Male&id=1234";
function removeURLParameter(url, parameter) {
//prefer to use l.search if you have a location/link object
var urlparts= url.split('?');
if (urlparts.length>=2) {

var prefix= encodeURIComponent(parameter)+'=';
var pars= urlparts[1].split(/[&;]/g);

//reverse iteration as may be destructive
for (var i= pars.length; i-- > 0;) {
//idiom for string.startsWith
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
pars.splice(i, 1);
}
}

url= urlparts[0]+'?'+pars.join('&');
return url;
} else {
return url;
}
}

console.log(removeURLParameter(url, 'name'));
console.log(removeURLParameter(url, 'gender'));

Jsfiddle example

Javascript - Remove query string from current url

Unfortunately i cant able to do it with javascript or jquery. So i go through with php redirection, now it works.

<?php
if(isset($_POST['Submit'])) {
// actions
if(isset($_REQUEST['key']) && ($_REQUEST['key'] != "")) {
header('Refresh: 1;url='.$_SERVER['PHP_SELF']);
}
}
?>

Thanks all :)

How can I remove the query string from the url after the page loads?

You can't do that without reloading the page, imagine if you could put whatever you wanted in the browser address bar? Security fun :)

Although you can now do it in HTML5 (which will only work on browsers supporting it) using the new history API, but realistically, your scenario best warrants a rewrite instead of including that (seems sledgehammer to crack a nut).

As you said you don't need the query string after the page loads, you should really post back, then redirected to another URL after you've finished your processing.

How to efficiently remove a query string by Key from a Url?

This works well:

public static string RemoveQueryStringByKey(string url, string key)
{
var uri = new Uri(url);

// this gets all the query string key value pairs as a collection
var newQueryString = HttpUtility.ParseQueryString(uri.Query);

// this removes the key if exists
newQueryString.Remove(key);

// this gets the page path from root without QueryString
string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

return newQueryString.Count > 0
? String.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
: pagePathWithoutQueryString;
}

an example:

RemoveQueryStringByKey("https://www.google.co.uk/search?#hl=en&output=search&sclient=psy-ab&q=cookie", "q");

and returns:

https://www.google.co.uk/search?#hl=en&output=search&sclient=psy-ab

Is there any method to get the URL without query string?

Try this: