【问题标题】:javascript window location href without hash?没有哈希的javascript窗口位置href?
【发布时间】:2011-08-14 15:59:40
【问题描述】:

我有:

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() 这个?

【问题讨论】:

  • 顺便说一句,如果您这样做只是为了链接到同一页面上的 #section,只需将链接 href 设置为 #section。您不需要获取页面的基本 url,然后在最后连接哈希。

标签: javascript location substring


【解决方案1】:

如果您不关心端口号或查询字符串,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 object

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

【讨论】:

  • 那里丢失了查询字符串(如果有的话)
  • 似乎 location.host 包括端口。
  • 关于.replace(location.hash,'') 的最后一点非常棒,正是我一直在寻找的。​​span>
  • location.href.replace(location.hash,"") 将无法正常工作,因为:example.com#example# 将给出 '' 作为哈希; example.com#example#a 将给出 '#a' 作为哈希; window.location.href.split('#')[0] 是正确的解决方案。
  • 第一个解决方案有一个问题:https://example.com/foo? 之类的 URL 将被转换为不带问号的 URL。有时这会导致问题。不过,您使用 URL 构造函数的最后一种方法似乎工作正常,谢谢!
【解决方案2】:
var uri = window.location.href.split("#")[0];

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

var hash = window.location.hash;

// Returns #hash

【讨论】:

  • 哈希符号不包含在该数组的第二部分中
  • 我确实包含了一个修复程序! :-)
  • 这不起作用。 "foo#bar#baz".split("#") == "bar"
  • 对于哈希,只需使用location.hash
【解决方案3】:
location.href.replace(location.hash,"")

【讨论】:

  • (location+'').href.replace(location.hash,"") 在 Firefox 中工作(位置不是常规字符串)
  • 但是当 url 是 somehting.com/# 时注意 location.hash 是 ''
  • 令我惊讶的是,如果location.hash 包含在 URL 的其他位置,这不会中断。例如,“example.com/#example”。因为location.hash 包含前导“#”。如上所述,当哈希为空白时除外。
  • location.hash 将包含从第一个 # 到最后的所有内容,并忽略任何子问题(非法)url 字符,如 #plungjan.name/SO/testhref.html - 我测试了,因为我的答案被否决了
【解决方案4】:

通用的方式也是越小吗?

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

【讨论】:

    【解决方案5】:

    更短的解决方案:

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

    • 仅无哈希location.href.split(location.hash||"#")[0]

    (我一般用第一个)

    【讨论】:

      【解决方案6】:

      ES2020:

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

      【讨论】:

        【解决方案7】:
        location.href = window.location.href.split("write here your code to delete in your URL")[0] + "write here your final destination";
        

        【讨论】:

        • 嗨 @Vittrix 欢迎来到 SO !请阅读How to answer,并尝试添加一些说明,说明您的答案与已接受的答案相比有何不同或更好!
        猜你喜欢
        • 2016-12-25
        • 2012-01-26
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 2013-07-30
        • 1970-01-01
        相关资源
        最近更新 更多