[英]Create a string/var/array from part of URL using javascript/jquery?
Looking to populate a view based on part of a URL. 根据URL的一部分来填充视图。
www.example.com/f/ objectID I have a website with a view that loads based on the an objectID that is stored on parse.com www.example.com/f/ objectID我有一个网站,其视图基于parse.com上存储的objectID加载
how can I extract/interpret " objectID " from the url to populate the /f/ view? 如何从URL中提取/解释“ objectID ”以填充/ f /视图?
Take a look at the window.location
object. 看一下window.location
对象。 From there you just need some string methods. 从那里,您只需要一些字符串方法。
var objectID = location.pathname.split('/').pop();
.split()
will split the path into the fragments separated by /
and .pop()
will retrieve the last one. .split()
会将路径拆分为用/
分隔的片段, .pop()
将检索最后一个片段。 Of course, if objectID
may not always be the last segment, use the proper (0-based) index to retrieve it from array returned by split
. 当然,如果objectID
不一定总是最后一个段,请使用适当的(从0开始)索引从split
返回的数组中检索它。
Another solution that may have better performance (shouldn't really make a difference for this use case), assumes that objectID
is the last segment: 另一个可能具有更好性能的解决方案(对于此用例而言,实际上并没有什么不同),假定objectID
是最后一个段:
var objectID = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
A regex would work or splitting the window.location.url: 正则表达式可以工作或拆分window.location.url:
var objectId = window.location.pathname.replace(/\/f\/(\w+)\/?/, '$1');
OR 要么
var objectId = window.location.pathname.split('/')[2];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.