如何将窗口.位置.路径名从对象"convert"为字符串?



我需要JS/jQuery中的路径名(www.my-site.com/this part/and this part/etc/),但我需要它作为string而不是对象。

换句话说,我需要JS/jQuery中的$_SERVER['REQUEST_URI'];

我试过:

var page_pathname = location.pathname;
var page_pathname = location.pathname + location.search;
var page_pathname = (location.pathname+location.search).substr(1);

console.log:

1.Object {error: Object}

2.Location {hash: "", search: "", pathname: "/my-cat/my-title/", port: "", hostname: "www.my-site.com"…}

我需要console.log:my-cat/my-title/

window.location.pathname已经是一个字符串。

您也可以尝试:

String(window.location.pathname)

这是对字符串的显式转换。

window.location.href还将帮助您检索完整的url。

使用toString()方法将对象转换为字符串

示例

var currentLocation = location.toString();
console.log(currentLocation);
o/p - "http://stackoverflow.com/posts/33895647/edit"

相关内容

最新更新