2011-05-28 66 views
2
window.location = 'http://...'; 

现在我想将这个位置路径分配给一个变量,作为一个普通的文本字符串。 我想要实现:Javascript echo window.location

var Path = 'http://...'; 

我试着使用:

var Path = window.location; 

,但我得到的,因为这VAR值:

function Path() { [native code] } 

,而我想有位置的文本字符串作为它的价值。

回答

2

是,window.location是一个对象,其href属性返回整个URL。

看到这里的location对象的引用(location的其他属性&功能会很有用):http://developer.mozilla.org/en/DOM/window.location

+2

应当指出的是,W3Schools的是不是最好的资源。有关信息,请参阅http://www.w3fools.com – Arjan 2011-05-28 19:36:11

+0

感谢您的评论,我不知道这一点。我更新了ref链接以指向MDC。 – 2011-05-29 20:19:39

5

你想要location.hreflocation对象比简单的字符串要复杂得多。

3

这应该工作(虽然我没有测试):

var path = window.location.href; 
0

您可以尝试

var Path = window.location.toString();