获取当前正在执行的JavaScript的网址 [英] Get Url For Currently Executing Javascript

查看:89
本文介绍了获取当前正在执行的JavaScript的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找当前正在执行的javascript的网址.我知道我可以在当前页面上使用window.location.href,但这不一定是正在执行的脚本的路径.

I'm trying to find the url for the currently executing javascript. I know I can use window.location.href for the current page, but that's not necessarily the path to the script which is executing.

非常感谢您的帮助.

谢谢.

我完全愿意使用插件等来实现此目的.不要犹豫,提出任何建议.

EDIT 1: I'm fully open to the use of plugins and such to accomplish this. Don't hesitate to suggest anything.

我需要这样做,以便可以找到从我当前正在执行的脚本所在的位置到另一个资源的相对路径.问题是,例如,脚本正在/js/myScript.js运行,而我需要参考的资源在/imgs/test.png.由于.js文件可以在任何地方包含,因此无法知道.png文件的路径.

EDIT 2: I need this so that I can find a relative path from where my currently executing script is at to another resource. The issue is that, for example, the script is running at /js/myScript.js and the resource I need to reference is at /imgs/test.png. Since the .js file can be included from anywhere, the path to the .png file cannot be known.

推荐答案

尝试一下,但是我不能保证它可以在我的Firefox中的所有浏览器中正常工作

Try this but i cant guarantee that it will work with all browsers in my firefox it worked:

var scripts = document.getElementsByTagName("SCRIPT");
var script  = scripts[scripts.length-1].src;

var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/);
alert(scriptName);

将警告脚本名称:

这篇关于获取当前正在执行的JavaScript的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆