我想/
在查询字符串的最后一个之后获取URL的一部分。
到目前为止,我具有URL的最后一部分,但是我无法从URL中删除查询字符串。
Javascript:
<script type='text/javascript'>
var url = window.location.href;
var array = url.split('/');
var lastsegment = array[array.length-1];
document.write(lastsegment);
</script>
URL的结构如下:
http://www.example.com/search/People?&results=latest
我只需要People
一部分。在上述Javascript中怎么可能?还是有更好的方法做到这一点?
尝试使用window.location.pathname
代替window.location.href
。它仅获取服务器名称和查询字符串之间的文本("/search/People"
在您的情况下):
var last_part = window.location.pathname.split('/').pop();
您可以pathname
在此处了解更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句