admin管理员组

文章数量:1134248

I have:

var uri = window.location.href;

That provides

What's the best and easiest way to get the entire path without the #hash?

uri    = 
nohash = 

I tried using location.origin+location.pathname which doesn't work in every browser. I tried using location.protocol+'//'+location.host+location.pathname which looks like kind of a crappy solution to me.

What is the best and easiest way to do so? maybe I query for location.hash and try to substr() this from the uri?

I have:

var uri = window.location.href;

That provides http://example.com/something#hash

What's the best and easiest way to get the entire path without the #hash?

uri    = http://example.com/something#hash
nohash = http://example.com/something

I tried using location.origin+location.pathname which doesn't work in every browser. I tried using location.protocol+'//'+location.host+location.pathname which looks like kind of a crappy solution to me.

What is the best and easiest way to do so? maybe I query for location.hash and try to substr() this from the uri?

Share Improve this question edited May 4, 2015 at 18:47 Phrogz 303k113 gold badges667 silver badges755 bronze badges asked Apr 28, 2011 at 12:02 mattmatt 44.2k106 gold badges268 silver badges402 bronze badges 1
  • 1 BTW, if your doing this just to link to a #section on the same page, just set the link href to #section. You don't need to get the page's base url then concatenate the hash on the end. – Web_Designer Commented Feb 25, 2013 at 6:59
Add a comment  | 

10 Answers 10

Reset to default 116

location.protocol+'//'+location.host+location.pathname is the correct syntax if you do not care about port number or querystring

If you do care:

https://developer.mozilla.org/en/DOM/window.location

location.protocol+'//'+
  location.host+
  location.pathname+
 (location.search?location.search:"")

or

location.protocol+'//'+
  location.hostname+
 (location.port?":"+location.port:"")+
  location.pathname+
 (location.search?location.search:"")

You can also just do a location.href.replace(location.hash,"")
It will remove EVERYTHING from the FIRST # and on regardless of other hash characters in the string

Alternatively create a URL object:

const url = new URL("https://www.somepage.com/page.html#anchor") //(location.href);
console.log(url)
url.hash="";
console.log(url)

var uri = window.location.href.split("#")[0];

// Returns http://example.com/something

var hash = window.location.hash;

// Returns #hash
location.href.replace(location.hash,"")

Is the universal way also the smaller?

location.href.split(/\?|#/)[0]

Shorter solutions:

  • without query string and hash location.href.split(location.search||location.hash||/[?#]/)[0]

  • only without hash location.href.split(location.hash||"#")[0]

(I usually use the first one)

I was looking for this answer:

`${window.location.origin}${window.location.pathname}${window.location.search}`

I'm a fan of letting native code do the heavy lifting as much as possible. I'd come to think that a browser can recognize the parts of URLs better than us doing a best guess. So here's another variant for your consideration:

new URL('#', location.href).slice(0, -1)

I guess this needs a litte explanation: What's happening is we're constructing a URL object with an empty hash on the current URL as base, i.e. if there is a hash, it gets replace by an empty one. We then remove the last character ('#') from the implicitly cast string (href of the URL object).

I prefer this short one-liners which directly return the wanted result instead of creating an array.

To remove only the hash (#hash):

location.href.replace(/#.*$/, '')

To remove both the hash (#hash) and the query params (?query=value&...):

location.href.replace(/(\?|#).*$/, '')

This is regex (/.../) for replacing from the first ? (\?) or ((...|...)) # (#), all characters (.*) until the end of the string ($), by an empty string ('').

ES2020:

let [uri, hash] = location.href.split("#");
console.log(uri, hash);

location.hash = "#myhash";

[uri, hash] = location.href.split("#");
console.log(uri, hash);

location.href = window.location.href.split("write here your code to delete in your URL")[0] + "write here your final destination";

本文标签: javascript window location href without hashStack Overflow