我目前在访问www.google.com/folder/folder/archive.php
,并使用window.location
来确定当前页面。实际上,我只想针对/archive.php
进行操作,而不涉及其他内容。
有什么方法可以实现吗?
window.location.host = "www.google.com"
window.location.pathname = "folder/folder/archive.php"
???? = "/archive.php
我目前在访问www.google.com/folder/folder/archive.php
,并使用window.location
来确定当前页面。实际上,我只想针对/archive.php
进行操作,而不涉及其他内容。
有什么方法可以实现吗?
window.location.host = "www.google.com"
window.location.pathname = "folder/folder/archive.php"
???? = "/archive.php
console.log(window.location.href.split('/').pop())
var a = window.location.pathname.split("/");
console.log(a[a.length - 1]);
console.log(a[-1]);
,为什么控制台会显示 undefined
? - user1469270试试这个
var path = "www.google.com/folder/folder/archive.php";
console.log(path.substring(path.lastIndexOf('/')+1));
这将从最后一个'/'选择部分。
var last =
window.location.toString().substr(window.location.toString().lastIndexOf('/'));
无论在哪个页面运行此脚本,最后的路径始终是相同的。
例如:http://www.example.com/../../../../../test.html
输出将为 test.html
http:// - Protocol
www - Server-Name (subdomain)
example - Second Level Domain (SLD)
com - Top Level Domain (TLD)
/test.html - Path
alert("/"+window.location.pathname.split("/")[2] );
www.google.com/folder/folder/archive.php
。我怀疑这个。example.com
是为了这个原因而保留的 :) - Gareth