【问题标题】:Is there any way to get ONLY the PHP page of the current page using javascript?有没有办法只使用 javascript 获取当前页面的 PHP 页面?
【发布时间】:2015-09-01 05:06:31
【问题描述】:
我想使用 javascript 获取我的确切位置(仅 PHP 页面),我目前正在使用 window.location 获取确切的 url 例如:localhost/empc/myfolder/functions.php 如果我只想获取 @987654326,代码是什么@ 因此?可能吗?谢谢。
【问题讨论】:
-
-
Here's PHP 的 basename 函数的 JS 实现。它会得到你需要的。
-
标签:
javascript
php
url
location
filenames
【解决方案1】:
您可以使用位置对象来做到这一点,要删除文件名前的“/”可以使用子字符串方法。
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
【解决方案2】:
你可以试试这个很简单的
var url = 'localhost/empc/myfolder/functions.php';
var url = url.split('/');
alert(url.pop());
【解决方案3】:
这是可能的。使用 javascript split function 将字符串分成几段,然后访问最后一个元素以获取文件名:
var str = "localhost/empc/myfolder/functions.php";
var arr = str.split("/");
alert(arr[arr.length-1]); //this will alert functions.php