获取当前脚本在javascript中执行文件名 [英] Getting the current script executing filename in javascript
本文介绍了获取当前脚本在javascript中执行文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,我一直在寻找这个问题已经太久了......我正在试图找出如何从javascript内部返回运行包含的javascript的页面的文件名。
Okay, I've been searching for this for way too long already... I'm trying to find out how I can return the filename of the page that's running an included javascript, from inside that javascript.
我可以使用 $ _ SERVER ['SCRIPT_FILENAME']
轻松地在PHP中执行此操作,但在Javascript中这似乎是要困难得多。
I can easily do this in PHP using $_SERVER['SCRIPT_FILENAME']
, but in Javascript this seems to be a lot harder.
我真的需要从 location.href
中对其进行子串,还是有更有效的方法? jQuery能帮忙吗?
Do I really need to substring it from location.href
or is there a more efficient way? Could jQuery help?
推荐答案
删除网址参数
function getFileName()
{
var url = window.location.pathname;
var lastUri = url.substring(url.lastIndexOf('/')+1);
if(lastUri.indexOf('?')!= -1)
return lastUri.substring(0,lastUri.indexOf('?'));
else
return lastUri;
}
这篇关于获取当前脚本在javascript中执行文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文