js,如何在location.hash中获得一个部分

时间:2011-09-05 08:52:09

标签: javascript

我希望获得location.hash部分,即%20之后的单词。但是怎么样?

解释

http://localhost/text/search.php?search=sweet%20girl  => girl
http://localhost/text/search.php?search=fashion  => NULL
http://localhost/text/search.php?search=2011%20best%20and%20worst => best and worst

2 个答案:

答案 0 :(得分:4)

var s = decodeURIComponent(location.search);
var index = s.indexOf(" ");
s = index === -1 ? s.substr(index + 1) : null;

答案 1 :(得分:0)

首先使用unescape功能。之后,您可以执行正则表达式replace

var url = "http://localhost/text/search.php?search=2011%20best%20and%20worst";
alert (unescape(url).replace(/^\S* /,""));