开发者

Determining relative location of a jQuery plugin

I'm developing a jQuery plugin that consists of a main js file that will load several other js files. I want to be able to put all the files into a 'myPlugin' folder, and have the main js file find the other scripts no matter what the location of the page that imports the main file.

location.pathname will get me the path of the page that loads the plugin, but how do I tell the plugin to load something relative to its own location?

In the following example, I want myPlugin.js to find required1.js whether it's loaded from pageA or pageB:

pageA.html
/../pageB.html

myPlugin/
  -myPlugin.js (How 开发者_StackOverflow社区does `myPlugin.js` find `required1.js`?)
  -required1.js
  -required2.js

Thanks...


The only thing I can think of is to try to find the reference to myPlugin.js in the DOM of the including page:

$('script[src$="myPlugin.js"]').attr("src").replace(/myPlugin\.js$/, 'required1.js')

This is hacky, though, and prone to error. I would only use it if a zero-conf solution is absolutely necessary. Otherwise, I would just have the user configure the plugin so it knows where to find its other files.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜