使用JavaScript获取URL的一部分

我目前在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]); 
 alert("/"+window.location.pathname.split("/")[2] ); 

试试这个

 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( ‘/’));

无论您运行此脚本的哪个页面,last都将始终是路径。

例如: http://www.example.com/../../../../../test.htmlhttp://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