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.
To get every part of the URL except for the query:
Note that this includes the hash as well, if there is one (I'm aware there's no hash in your example URL, but I included that aspect for completeness). To eliminate the hash, simply exclude
.concat(location.hash)
.It's better practice to use
concat
to join Javascript strings together (rather than+
): in some situations it avoids problems such as type confusion.Read about
Window.location
and theLocation
interface:just cut the string using split (the easy way):
Try:
(NB:
.host
rather than.hostname
so that the port gets included too, if necessary)Here are two methods:
How about this:
location.href.slice(0, - ((location.search + location.hash).length))