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

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

I have a URL like http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.
I want to get the URL without the query string: http://localhost/dms/mduserSecurity/UIL/index.php.
Is there any method for this in JavaScript? Currently I am using document.location.href, but it returns the complete URL.

Solutions/Answers:

Solution 1:

Try this: window.location.href.split('?')[0]

Solution 2:

Read about Window.location and the Location interface:

var url = [location.protocol, '//', location.host, location.pathname].join('');

Solution 3:

location.toString().replace(location.search, "")

Solution 4:

var url = window.location.origin + window.location.pathname;

Solution 5:

If you also want to remove hash, try this one: window.location.href.split(/[?#]/)[0]

Solution 6:

Try:

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

(NB: .host rather than .hostname so that the port gets included too, if necessary)

Related:  How can I copy to clipboard in HTML5 without using flash?