没有哈希的javascript窗口位置href?

发布于 2024-11-04 01:05:31 字数 526 浏览 2 评论 0原文

我有:

var uri = window.location.href;

这提供了 http://example.com/something#hash

在没有 #hash 的情况下获取整个路径的最佳和最简单的方法是什么?

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

我尝试使用 location.origin+location.pathname ,但它并不适用于每个浏览器。我尝试使用 location.protocol+'//'+location.host+location.pathname 这对我来说看起来像是一个蹩脚的解决方案。

最好和最简单的方法是什么?也许我查询 location.hash 并尝试从 uri 中 substr() ?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

妄想挽回 2024-11-11 01:05:31

如果您不关心端口号或查询字符串,则 location.protocol+'//'+location.host+location.pathname 是正确的语法

如果您确实关心:

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

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

或者

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

您也可以执行 <代码>location.href.replace(location.hash,"")
它将删除从第一个 # 开始的所有内容,无论字符串中的其他哈希字符如何

。或者创建一个 URL 对象

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

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)

哥,最终变帅啦 2024-11-11 01:05:31
var uri = window.location.href.split("#")[0];

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

var hash = window.location.hash;

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

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

var hash = window.location.hash;

// Returns #hash
┊风居住的梦幻卍 2024-11-11 01:05:31
location.href.replace(location.hash,"")
location.href.replace(location.hash,"")
近箐 2024-11-11 01:05:31

万能的方式也是越小越好吗?

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

Is the universal way also the smaller?

location.href.split(/\?|#/)[0]
零度° 2024-11-11 01:05:31

较短的解决方案:

  • 没有查询字符串和哈希location.href.split(location.search||location.hash||/[?#]/)[0]

  • 仅无哈希 location.href.split (location.hash||"#")[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)

葬﹪忆之殇 2024-11-11 01:05:31

我一直在寻找这个答案:

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

I was looking for this answer:

`${window.location.origin}${window.location.pathname}${window.location.search}`
木落 2024-11-11 01:05:31

我喜欢让本机代码尽可能地完成繁重的工作。我开始认为浏览器可以比我们做出最佳猜测更好地识别 URL 的各个部分。所以这里有另一种变体供您考虑:

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

我想这需要一点解释:正在发生的事情是我们正在构建一个 URL 对象,其中当前 URL 上有一个空哈希作为基础,即如果有一个哈希,它会被一个空替换一。然后,我们从隐式转换字符串(URL 对象的 href)中删除最后一个字符(“#”)。

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).

口干舌燥 2024-11-11 01:05:31

我更喜欢这种简短的单行代码,它直接返回想要的结果,而不是创建一个数组。

仅删除哈希 (#hash):

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

同时删除哈希 (#hash) 和查询参数 (?query=value&...):

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

这是正则表达式 (/.../) 用于从第一个替换 ? (\?) 或 ((...|...)) # (#),所有字符 (.*) 直到字符串末尾 ($),由空字符串 ('') 组成。

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 ('').

七七 2024-11-11 01:05:31

ES2020:

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

location.hash = "#myhash";

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

ES2020:

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

location.hash = "#myhash";

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

软的没边 2024-11-11 01:05:31
location.href = window.location.href.split("write here your code to delete in your URL")[0] + "write here your final destination";
location.href = window.location.href.split("write here your code to delete in your URL")[0] + "write here your final destination";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文