开发者

Relative path of loaded javascript

I am developing a jQuery plugin which needs to load other files relative to the same director开发者_运维知识库y in which my 'plugin.js' file is located. Is there a way to do this?


One way to achieve this is to loop through all $('script') elements, identify the one that represents your plugin inclusion, and extract the path. Of course this might not be reliable as the consumer of your plugin might have renamed your .js file.

Actually what you have is a pretty weird requirement. Can't you simply describe in the documentation of the plugin which scripts need to be included or simply pass the relative url as argument to your plugin? For example many jQuery plugins take a url as parameter so that the consumer can specify where did he put other required scripts. This allows for the consumer more flexibility on the location of those external scripts.


Always use a "/" in front of the path you are trying to locate. A "/" means search at the root directory for instance:

<img src="images/image.png"> // Will search for a directory inside the current directory.
<img src="/images/image.png">      // Will search for a directory in the "Root" path.

The first example searches for a directoy relative to the current directory, the 2nd example searches for a directory located in the root of the hosted site.


You have to be sure your script is called as the first one on the page

var scripts = document.getElementsByTagName("script");
var thisScript = scripts[0];
var thisScriptsSrc = thisScript.src;

it's src of an actual javascript and you can do whatever you need with that

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜