Javascript 如何";转换;从对象到字符串的window.location.pathname?
我需要JS/jQuery中的路径名(www.my-site.com/this part/和this part/etc/),但我需要它作为字符串而不是对象 换句话说,我需要Javascript 如何";转换;从对象到字符串的window.location.pathname?,javascript,jquery,pathname,Javascript,Jquery,Pathname,我需要JS/jQuery中的路径名(www.my-site.com/this part/和this part/etc/),但我需要它作为字符串而不是对象 换句话说,我需要$\u服务器['REQUEST\u URI'] 我试过: var page_pathname = location.pathname; var page_pathname = location.pathname + location.search; var page_pathname = (location.pathname
$\u服务器['REQUEST\u URI']
我试过:
var page_pathname = location.pathname;
var page_pathname = location.pathname + location.search;
var page_pathname = (location.pathname+location.search).substr(1);
我从控制台.log中得到的一切
1.对象{错误:对象}
2.位置{hash:,搜索:,路径名:“/my cat/my title/”,端口:,主机名:“www.my-site.com”…}
我需要的console.log
:my cat/my title/
使用toString()
方法将对象转换为字符串
范例
var currentLocation = location.toString();
console.log(currentLocation);
o/p - "http://stackoverflow.com/posts/33895647/edit"
window.location.pathname
已经是一个字符串
您也可以尝试:
字符串(window.location.pathname)
这是到字符串的显式转换
window.location.href
也将帮助您检索完整的url。我不确定您的问题,因为location.pathname
返回一个字符串,而不是一个对象console.log(location.pathname.substr(1))
应该适合您。@Solo无论您尝试什么,您都尝试错了.pathname
始终是一个字符串。@Solo它不是答案。如果这解决了问题,那么这个问题可能应该被删除或关闭,因为问题是由于语法或排版问题。