当我尝试解析变量时,.split()将无效

时间:2014-02-21 23:24:20

标签: javascript

我正在将window.location变成一个变量而我正在尝试拆分它,但它无效。有谁知道为什么?

如果我alert(window.location)它给了我:

http://localhost/instagramAjax.php#access_token=asd7.032033f.90c7de9easdab96d880799c34d10f

所以我这样做,但它不起作用。它只会杀死我的javascript。

var token = window.location;
token = token.split("#access_token=");
alert(token[1]);

2 个答案:

答案 0 :(得分:5)

位置在技术上是一个对象,使用window.location.href,这是字符串值。 Here's more info if you're interested.

答案 1 :(得分:0)

如果你想在'#'之后阅读片段字符串,而不是解析location.href,你可以简单地使用它:

window.location.hash

同样,您可以使用以下内容在“?”之后读取查询字符串:

window.location.search