开发者

Use multiple jQuery versions in a Joomla site without renaming jQuery

I know my question is similar to many others, but I hope the specifics are different enough to justify a new question. A joomla site I'm working on uses mod_superfishmenu. This loads jQuery 1.2.6. and extends it. I'm creating a component that uses the jQuery colorbox plugin that requires jQuery 1.3 or higher and again extends JQuery. One technique to have both is reassign jQuery to another name with jQuery.noConflict(). For instance,

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js'>
</script>
<script type='text/javascript'>
var jQuery1 = jQuery.noConflict(true);
</script>

and then change all instances of jQuery in the JavaScript code for colorbox to jQuery1, and make sure all this loads before the superfish module loads its jQuery. But I don't like having to edit the colorbox code, or remembering to edit it again if I upgrade to a newer version. So, I thought it might be possible to load the older jQuery first, then use jQuery.noConflict(true) to rename it, then load the current jQuery, and transfer the plugins attached to the first jQuery to the second one. The part that copies the plugins looks like this:

<script type='text/javascript'>
var oldJquery = null;
if (jQuery != undefined) {
  oldJquery = jQuery.noConflict(true);
}
</script>

<script type='text/javascript'  src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js'>
</script>

<script type='text/javascript'>
jQuery.noConflict();
if (oldJquery) {
  for (plugin in oldJquery.fn) {
    if (!(pl开发者_如何转开发ugin in jQuery.fn))
      jQuery.fn[plugin] = oldJquery.fn[plugin];
  }
}
</script>

This seems to work, but I'm concerned that I've overlooked some detail that can bite me later on. So if anyone sees a flaw in this approach, please point it out. I also welcome any critique that shows why this is a bad idea. Thanks.


Why not make a simple change to mod_superfishmenu and remove the line that include jQuery 1.2.6? This way your jQuery colorbox plugin will load jQuery 1.3 itself which mod_superfishmenu will also use. Simple one line change that is not difficult to do.

As a side note I think all developers for Joomla that include any javascript library should have backend settings in order for users to enable or disabled any library should they have conflicts.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜