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

JavascriptUrl

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

Try this:

let path = window.location.href.split('?')[0]
console.log({path})

Solution 2 - Javascript

Read about Window.location and the Location interface:

const urlPieces = [location.protocol, '//', location.host, location.pathname]
let url = urlPieces.join('')

console.log({urlPieces, url})

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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

Solution 6 - Javascript

Here's an approach using the URL() interface:

new URL(location.pathname, location.href).href

Solution 7 - Javascript

Try:

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

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

Solution 8 - Javascript

just cut the string using split (the easy way):

var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);

Solution 9 - Javascript

To get every part of the URL except for the query:
var url = (location.origin).concat(location.pathname).concat(location.hash);

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.

Solution 10 - Javascript

Use properties of window.location

var loc = window.location;
var withoutQuery = loc.hostname + loc.pathname;
var includingProtocol = loc.protocol + "//" + loc.hostname + loc.pathname;

You can see more properties at https://developer.mozilla.org/en/DOM/window.location

Solution 11 - Javascript

How about this: location.href.slice(0, - ((location.search + location.hash).length))

Solution 12 - Javascript

Here are two methods:

<script type="text/javascript">
	var s="http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu
                                =true&pcode=1235";

	var st=s.substring(0, s.indexOf("?"));

	alert(st);

	alert(s.replace(/\?.*/,''));
</script>

Solution 13 - Javascript

Just add these two lines to $(document).ready in JS as follow:

$(document).ready(function () {
 $("div.sidebar nav a").removeClass("active");
        $('nav a[href$="'+ window.location.pathname.split("?")[0] +'"]').addClass('active');
});

it is better to use the dollar sign ($) (End with)

$('nav a[href$

instead of (^) (Start with)

$('nav a[href^

because, if you use the (^) sign and you have nested URLs in the navigation menu, (e.g "/account" and "/account/roles")

It will active both of them.

Solution 14 - Javascript

window.location.href.split("#")[0].split("?")[0]

Solution 15 - Javascript

You could make use of the URL constructor like this:

const href = 'http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235'; // document.location.href
const url = new URL(href);
const noSearchUrl = href.replace(url.search, '');
console.log(noSearchUrl);

Solution 16 - Javascript

If you are using navigation bar and want to get the pure url after clicking on the side bar navigation, then the following code might be helpful:

$(document).ready(function () {
    $("div.sidebar nav a").removeClass("active");
    var urlPath = window.location.pathname.split("?")[0];
    var nav = $('div.sidebar nav a').filter(function () {
        return $(this).attr('href').toLowerCase().indexOf(urlPath.toLocaleLowerCase()) > -1;
    });
    $(nav).each(function () {
        if ($(this).attr("href").toLowerCase() == urlPath.toLocaleLowerCase())
            $(this).addClass('active');
    });
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsaintView Question on Stackoverflow
Solution 1 - JavascripttradyblixView Answer on Stackoverflow
Solution 2 - JavascriptFelix KlingView Answer on Stackoverflow
Solution 3 - JavascriptQuentinView Answer on Stackoverflow
Solution 4 - JavascriptJasonView Answer on Stackoverflow
Solution 5 - Javascriptuser1079877View Answer on Stackoverflow
Solution 6 - Javascripts4yView Answer on Stackoverflow
Solution 7 - JavascriptAlnitakView Answer on Stackoverflow
Solution 8 - JavascriptpleasedontbelongView Answer on Stackoverflow
Solution 9 - JavascriptAndrew FaulknerView Answer on Stackoverflow
Solution 10 - JavascriptdetaylorView Answer on Stackoverflow
Solution 11 - JavascriptLI XiangChenView Answer on Stackoverflow
Solution 12 - JavascriptTheVillageIdiotView Answer on Stackoverflow
Solution 13 - Javascripti Smart LearningView Answer on Stackoverflow
Solution 14 - JavascriptBaiJiFeiLongView Answer on Stackoverflow
Solution 15 - JavascriptTudor MorarView Answer on Stackoverflow
Solution 16 - Javascripti Smart LearningView Answer on Stackoverflow