javascript窗口位置href没有哈希? [英] javascript window location href without hash?

查看:173
本文介绍了javascript窗口位置href没有哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

var uri = window.location.href;

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

在没有 #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

我尝试使用 location.origin + location.pathname 在每个浏览器中都不起作用。我尝试使用 location.protocol +'//'+ location.host + location.pathname 这对我来说看起来像是一个糟糕的解决方案。

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.

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

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

推荐答案

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

这篇关于javascript窗口位置href没有哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆