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 s
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.
If you also want to remove hash, try this one: window.location.href.split(/[?#]/)[0]
var url = window.location.origin + window.location.pathname;
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
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>
Try this: window.location.href.split('?')[0]