How to get url path without $_GET parameters using javascript?

javascript, jquery, url

Solution

Don't do this regex and splitting stuff. Use the browser's built-in URL parser.

window.location.origin + window.location.pathname

And if you need to parse a URL that isn't the current page:

var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
console.log(url.origin + url.pathname);

And to support IE (because IE doesn't have `location.origin`):

location.protocol + '//' + location.host + location.pathname;

(Inspiration from https://stackoverflow.com/a/6168370/711902)

Problem

If my current page is in this format... ``` http://www.mydomain.com/folder/mypage.php?param=value ``` Is there an easy way to get this ``` http://www.mydomain.com/folder/mypage.php ``` using javascript?

Original source

Related problems