5

How can I remove a query string from the url of a link? For example I have

<a href="http://example.com/somepath/anotherpath?title=dog">The Link</a>

How can I remove just the ?title=dog from the url using javascript/jquery?

Dustin
  • 4,314
  • 12
  • 53
  • 91

6 Answers6

14

You can remove the query string from a link by simply changing the search property of the Location object.

$("#your_link")[0].search = "";

Demo: http://jsfiddle.net/uSrmg/1/

Or if you need to target multiple elements:

$("a.someclass").each(function() {
 this.search = "";   
});

Demo: http://jsfiddle.net/uSrmg/4/


If you wish to parse an arbitrary URL to remove the query string, you can inverse the trick explained in this post. For example:

function remove_qs(url) {
    var a = document.createElement('a'); // dummy element
    a.href = url;   // set full url
    a.search = "";  // blank out query string
    return a.href;
}

This should be quite robust as it uses the Location object to do all the parsing.

Example usage: http://jsfiddle.net/uSrmg/


Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • Ok this looks really close, but how can I get it to work if the url has a path in it like in my updated example? – Dustin Jul 18 '12 at 14:42
  • Oops.. yeah it is. Now my issue is that I have a bunch of links on my page that I need to remove the query string from. The code in your first example is only working on the first link. – Dustin Jul 18 '12 at 14:45
  • There ya go! That will do it :) Thank you so much! – Dustin Jul 18 '12 at 14:51
3

To remove the querystring from an anchor tag:

var anchor = $('#yourAnchor');
var href = anchor.attr('href');

if (href.indexOf('?') !== -1) {
   anchor.attr('href', href.split('?')[0]);
}
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • Checking that the `href` attribute contains a `?` character is unnecessary - `.split()` will simply return an array with a single element (the entire string) if the separator isn't present. – Anthony Grist Jul 18 '12 at 14:26
  • @AnthonyGrist until another developer decides to copypasta this code and use it elsewhere, introducing a bug. if anything, there should be a comment about this behavior instead of an if statement. – jbabey Jul 18 '12 at 14:28
  • 1
    If they do that's **their** problem, and in my opinion a very poor reason for including unnecessary checks in your code. – Anthony Grist Jul 18 '12 at 14:36
3

There is one more way to replace search with blank.

String(document.location.href).replace("&title=dog", "");
Alok Jha
  • 562
  • 7
  • 22
2
var url=$('a').attr('href').split('?')[0];

Split on the "?" and get the first item. If you want to change the url of all links on the page then you can use this code:

$('a').each(function(i,el){ 
    $(el).attr('href', $(el).attr('href').split('?')[0]);
});
Willem
  • 5,364
  • 2
  • 23
  • 44
  • this will give you the base URL, not the querystring - you meant `[1]`, but you probably want to add some bullet-proofing in case the `href` has no `?` character. – jbabey Jul 18 '12 at 14:18
  • 2
    @jbabey They want to remove the querystring, so returning the base URL is correct. – Anthony Grist Jul 18 '12 at 14:20
  • Sorry, there will be a path in the url too. I've updated my question to reflect that. – Dustin Jul 18 '12 at 14:20
  • Hmmm I'm getting this error for some reason.. Uncaught TypeError: Cannot call method 'indexOf' of undefined (anonymous function) (anonymous function) – Dustin Jul 18 '12 at 14:30
  • There is no indexOf in my code, did you mean to respond to jbabey's answer? My function works: http://jsfiddle.net/2jqWA/ – Willem Jul 18 '12 at 18:32
0

You can split the query string from the url on page load as:-

$(function(){
  if(Modernizr.history)
  {
    history.replaceState({},"",location.href.split("?")[0]);
  }
});
Ghanshyam Singh
  • 1,361
  • 2
  • 15
  • 27
0

This code worked for me:

var url = "www.foo.com/test?name=kevin&gender=Male&id=1234";

var res = url.replace("&gender=Male", "");

window.location.href =res;

Split particular string query parameter in url by javascript

Clíodhna
  • 818
  • 1
  • 15
  • 29