0

How would I remove part of a URL with jQuery, when the said part is changeable?

E.g. how can I remove this from my URL: ?modal=name

when 'name' might be name1 name2 or name3

I guess I will need to split the URL, but how do I do this when the modal name might be anything?

caustic
  • 585
  • 2
  • 8
  • 18
  • Wouldn't you just replace the modal name value with name1, name2,...? – Peter Mar 25 '15 at 07:56
  • Just in case you only want to remove the `modal` parameter: http://stackoverflow.com/questions/1634748/how-can-i-delete-a-query-string-parameter-in-javascript – Salman A Mar 25 '15 at 08:16

2 Answers2

1
var url = 'http://yourdomain.com/search?modal=name';

alert(url.substring(0, url.indexOf('?')));

Demo

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

As you have asked "How to remove the part of a URL" using jQuery

Here is a basic workaround for you:

//the URL ( decode it just for GOOD practice)
var someRandomUrl = decodeURI("http://example.com?modal=name");

//here we do the splitting
var splittedParts = someRandomUrl.split("?");

// the first part of the array will be URL you will require
var theURL = splittedParts[0];

// the second part of the array will be the Query String
var theQuery = splittedParts[1];
alert(theURL);
Hammad
  • 1,268
  • 15
  • 27