Javascript echo window.location
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6163163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript echo window.location
提问by serytankian
window.location = 'http://...';
now I want to assign this location path to a variable, as a normal text string. I want to achieve:
现在我想将此位置路径分配给一个变量,作为一个普通的文本字符串。我想实现:
var Path = 'http://...';
i tried to use:
我尝试使用:
var Path = window.location;
but I get, as this var value:
但我明白了,因为这个 var 值:
function Path() { [native code] }
while I want to have the location text string as its value..
而我想将位置文本字符串作为其值..
回答by Quentin
You want location.href
. The location
object is rather more complicated than a simple string.
你要location.href
。该location
对象比简单的字符串要复杂得多。
回答by Arjan
This should work (though I didn't test):
这应该有效(虽然我没有测试):
var path = window.location.href;
回答by Sébastien RoccaSerra
Yes, window.location
is an object, its href
property returns the entire URL.
是的,window.location
是一个对象,它的href
属性返回整个 URL。
See here for a reference on the location
object (location
's other properties & functions can be useful): http://developer.mozilla.org/en/DOM/window.location
请参阅此处以获取有关location
对象的参考(location
的其他属性和功能可能很有用):http: //developer.mozilla.org/en/DOM/window.location
回答by khrizenriquez
You can try
你可以试试
var Path = window.location.toString();