2012-04-24 62 views
1

我需要通过Javascript解析给定的url,但我无法使用.split()完成此操作。使用Javascript查找字符串中的“#”字符

IE:

var str = window.location.pathname; 
    var substr = str.split('/'); 
    alert(substr) 

如果我进入这个网址 “http://mydomain.com/myaddress/page/2/” 我有thoose价值观 “myaddress,页2”

但它不工作,如果不是/我插入#

+2

确定'location.pathname'包含''#?通常以'location.hash'命名。 – Thilo 2012-04-24 12:59:56

+0

可能的重复[从网址抓取哈希?](http://stackoverflow.com/questions/9043067/grabbing-hash-from-url) – 2012-04-24 13:04:28

回答

4

使用window.location.hash,它给你的部分分解。

而且,你要对#字符“分裂”,不会在window.location.pathname发现,只有--unless您使用window.location和搜索整个网址window.location.hash

1

您需要使用location.hash财产