2016-08-18 144 views
-1

如果我的网址是www.google.co.uk/test#number1,有没有办法将 window.location.pathname; 转变为读取为test#number1的变量,而不是终止于“#”散列符号的变量?有没有办法将一个带有散列的URL传递给一个javascript变量?

在此先感谢。

编辑(更多信息)。

我期望实现的是使用包含散列的当前URL的路径名填充文本框。

riscarrott的答案完美的作品,但因为我有< 15代表我不能投票它。 :(

document.getElementById("textbox").innerHTML = window.location.pathname + window.location.hash;

+0

你想读取当前网址或者你想设置一个带有标签的网址? –

+2

这个问题并不完全清楚你在问什么,你认为什么东西会突然截断字符串##?? –

+0

无法复制'location.hash '继续ains所有哈希。 – Oriol

回答

0

您可以将地点对象上读取哈希太:

// https://www.google.co.uk/test#number1 
var foo = window.location.pathname + window.location.hash; 
console.log(foo) // "/test#number1" 

MDN提供的BOM良好的文档 - https://developer.mozilla.org/en-US/docs/Web/API/Window/location

+0

这很好用,非常感谢! – Andrew

+0

URI规范不支持多个散列片段 - https://tools.ietf.org/html/rfc3986#section-3.5 – riscarrott

+0

虽然在Chrome中进行测试,但我可以看到'https://www.google.co。 uk/test#number1#number2'does not work so window.location.hash' ==='#number1#number2' – riscarrott

相关问题