开发者

How to access jQuery after jQuery.noConflict(); - not namespaced

开发者 https://www.devze.com 2023-04-11 04:50 出处:网络
I am in big trouble at the moment. We have a huge JS library that I need to maintain. It uses internally jQuery 1.6.2.

I am in big trouble at the moment. We have a huge JS library that I need to maintain. It uses internally jQuery 1.6.2. The client where we installed the library uses jQuery 1.3.4 and the fancybox overlay plugin. After loading these two, he simply throws in a

jQuery.noConflict();

but without saving his jQuery to a variable (namespacing).

Now I need to access his fancybox, but if I use

$.fancybox({...})

or

jQuery.fancybox({...})

I get in both cases an "is not a method error".

I can duplicate the error on my local machine and it would not appear without the jQuery.noConflict(); statement.

We are also doing a noConflict with our jQuery but we save it to another varieable, i.e.

jq162 = jQuery.noConflict();

The problem is the customer is of course unwilling to change anything of his code. Is there any way how I can access his jQuery / Fancy after this statement an开发者_JAVA技巧d after loading our 1.6.2?

thanks in advance...

UPDATE

the scripts are loaded in the following order:

// client

  1. jquery 1.4.2

  2. jquery fancybox

  3. <script type="text/javascript"> jQuery.noConflict(); </script>

  4. jQuery 1.2.6 which seems to be necessary for Liferay

// now comes my library

  1. jQuery 1.6.2

  2. my scripts

i know, if we could change step 3 to

<script type="text/javascript"> $jq = jQuery.noConflict(); </script> it would work, but right now that is out of my influence.

in 6. myscripts I need to access the fancybox from 2.

any ideas?


It shouldn't be a problem. You must be loading your scripts after the client's scripts (if you're loading yours first, there shouldn't be any problem, your jquery is namespaced, and the clients version will be in jQuery along with the plugin).

So simply namespace his jQuery object before you load your script:

<script>
    jq132 = jQuery;
</script>
<script src="yourScripts"></script>
<script>
    jq162 = jQuery.noConflict();
    console.log(jq132.fancybox);
</script>

UPDATE

As per your update, what you're trying to do is impossible. There is no longer a reachable reference to that jQuery/plugin instance (unless fancybox accidentally leaked a global reference, which I highly doubt). I don't know fancybox, although it's possible that the functionality isn't instance-specific. So it may be possible to just reattach fancybox to your version of jquery, and it will be able to perform all the necessary things. What I said about the reference however, remains true.

Obviously adding a few characters like you suggested (or other similar ways) would solve the problem. But if that is impossible, then your client will have to realise that. It should be proof enough if you simply ask them to access there own plugin under the same conditions - i.e. without changing code.

They should probably have a long and hard think about their entire project. Having to load three different versions of the same product is a sign that something is very very wrong.

0

精彩评论

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