If I use:
alert[window.location.href];
I get everything including query strings. Is there a way to just get the main url part, for example:
//mysite.com/somedir/somefile/
instead of
//mysite.com/somedir/somefile/?foo=bar&loo=goo
asked Jun 6, 2011 at 20:10
1
This is possible, but you'll have to build it manually from the location
object:
location.protocol + '//' + location.host + location.pathname
answered Jun 6, 2011 at 20:12
lonesomedaylonesomeday
227k49 gold badges310 silver badges313 bronze badges
12
Every answer is rather convoluted. Here:
var url = window.location.href.split['?'][0];
Even if a ? isn't present, it'll still return the first argument, which will be your full URL, minus query string.
It's also protocol-agnostic, meaning you could even use it for things like ftp, itunes.etc.
answered Feb 22, 2015 at 19:31
OddmanOddman
3,4211 gold badge15 silver badges12 bronze badges
8
I'm LATE to the party, but I had to solve this recently, figured I'd share the wealth.
const url = window.location.origin + window.location.pathname
////example.com/somedir/somefile/
window.location.origin
will give you the base url, in our test case: //example.com
window.location.pathname
will give you the route path [after the base url], in our test case /somedir/somefile
SOLUTION 2
You can simply do the following to get rid of the query parameters.
const url = window.location.href.split['?'][0]
answered Sep 23, 2020 at 14:32
Robo RickRobo Rick
5915 silver badges7 bronze badges
0
Use indexOf
var url = "//mysite.com/somedir/somefile/?aa";
if [url.indexOf["?"]>-1]{
url = url.substr[0,url.indexOf["?"]];
}
answered Jun 6, 2011 at 20:13
NiklasNiklas
29.5k5 gold badges48 silver badges70 bronze badges
1
You can concat origin
and pathname
, if theres present a port such as example.com:80
, that will be included as well.
location.origin + location.pathname
answered Mar 27, 2019 at 12:00
AamirRAamirR
10.9k4 gold badges52 silver badges69 bronze badges
1
Just one more alternative using URL
var theUrl = new URL[window.location.href];
theUrl.search = ""; //Remove any params
theUrl //as URL object
theUrl.href //as a string
answered Sep 22, 2020 at 22:27
digitalWestiedigitalWestie
2,5475 gold badges27 silver badges41 bronze badges
Use the URL[]
constructor, then extract and concatenate the origin and pathname. This will automatically strip the search [aka query] parameters from the url, leaving the scheme, domain, port and pathname only.
const url = new URL['//localhost:8080/index.html?search=foo&other=bar'];
console.log[url.origin + url.pathname];
As a note, this type of transformation is usually referred to as normalization, specifically in this case URI Normalization. There may already exist libraries that accomplish this more robustly with more options in your environment.
answered Jan 17 at 10:14
zr0gravity7zr0gravity7
2,4911 gold badge9 silver badges26 bronze badges
var url = "tp://mysite.com/somedir/somefile/?foo=bar&loo=goo"
url.substring[0,url.indexOf["?"]];
answered Jun 6, 2011 at 20:13
HeadshotaHeadshota
20.5k11 gold badges59 silver badges79 bronze badges
1
You can use a regular expression: window.location.href.match[/^[^\#\?]+/][0]
answered Jun 6, 2011 at 20:25
MicMic
24.4k9 gold badges56 silver badges69 bronze badges
If you look at the documentation you can take just the properties you're interested in from the window
object i.e.
protocol + '//' + hostname + pathname
answered Jun 6, 2011 at 20:14
planetjonesplanetjones
12.3k5 gold badges49 silver badges51 bronze badges
1