Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

url

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.

like image 593
saint Avatar asked Apr 28 '11 10:04

saint


People also ask

Does URL include query string?

A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.

Are URL parameters always strings?

Yes, URL query string params are of type string. It's up to you to convert them to and from the type you need.


2 Answers

Try this:

let path = window.location.href.split('?')[0] console.log({path})
like image 183
tradyblix Avatar answered Sep 19 '22 18:09

tradyblix


Read about Window.location and the Location interface:

const urlPieces = [location.protocol, '//', location.host, location.pathname] let url = urlPieces.join('')  console.log({urlPieces, url})
like image 33
Felix Kling Avatar answered Sep 21 '22 18:09

Felix Kling