我有以下网址 www.webiste.com/Services/allservices.html
我怎么能用jQuery获取这个URL的一部分?
例如' services'
或'allservices.html'
任何建议都非常感谢.
Sot*_*ris 13
var url = "www.webiste.com/Services/allservices.html"
url = url.split("/");
alert(url[1]);
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,url在find /(分隔符)时拆分字符串并将其保存在数组中.然后使用正确的索引,您可以使用您希望的子字符串(在上面的示例中将提醒Services).
更多信息split():https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split
var url = 'http://www.website.com/Services/allservices.html';
// Getting the file name (allservices.html)
var fn = url.split('/').reverse()[0];
// Getting the extension (html)
var ext = url.split('/').reverse()[0].split('.').reverse()[0];
// Getting the last middle part (services)
var lm = url.split('/').reverse()[1];
Run Code Online (Sandbox Code Playgroud)
如果您使用的是JavaScript window.location,则可以location.pathname使用"/" 进行拆分,并使用它pop()来获取最后一个元素或按索引获取:
var splitUrlArray = urlString.split('/');
var lastPart = splitUrlArray.pop();
var firstPart = splitUrlArray[0];
Run Code Online (Sandbox Code Playgroud)
阅读http://davidwalsh.name/javascript-window-location了解更多详情window.location.
| 归档时间: |
|
| 查看次数: |
22115 次 |
| 最近记录: |