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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:42:22  来源:igfitidea点击:

Javascript echo window.location

javascriptstringwindow.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 locationobject 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.locationis an object, its hrefproperty returns the entire URL.

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

See here for a reference on the locationobject (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();