prototype and jQuery peaceful co-existence?

前端 未结 4 899
清酒与你
清酒与你 2021-02-08 07:22

I know very little about JavaScript but despite this I\'m trying to cobble something together on my wordpress blog. It\'s not working, and I don\'t know how to resolve it, and h

相关标签:
4条回答
  • 2021-02-08 08:06

    Its worth reading this article on the JQuery site about Using JQuery With Other Libraries. It deals with more than just the noConflict option.

    0 讨论(0)
  • 2021-02-08 08:11

    There is a nasty trick many libraries do that I've taken a distinct liking to, and it looks like prototype is one of these.

    Mootools does this, If I am right, and it involves overloading many of the prototypes on the basic classes, monkey patching them.

    And likewise, I similarly encountered strange behaviour when mootools and jQuery were present, usually jQuery dying because it was calling some object method which had been somehow overloaded/monkey patched by Mootools.

    Also, mysteriously, taking mootools out of the script usage list, resulted in everything running much faster, which I concluded was due to less object pollution.

    Now I could be wrong, but I concluded from my experience such libraries just simply don't like to co-exist with each other, and seeing how mootools code seemed to me to degrade speed at which normal things were done, I sucked up and ported all mootools based code to jQuery ( A time consuming deal I assure you ), and the result, was code that was fast and didn't have weird errors that were unexplainable.

    I recommend you consider migration as at least One of your options.

    One More thing, when writing:

    I tend to use this syntax with all my jQuery driven code, for a bit of safe encapsulation in the event somebody breaks '$' somehow.

    Runtime Code This waits for document.ready before executing:

     jQuery(function($){ 
          code_with_$_here; 
     }); 
    

    jQuery Plugins

    (function($){ 
        code_with_$_here; 
    })(jQuery); 
    

    Using these will make it easier for people using any jQuery you happen to write to be able to use it without much of a conflict issue.

    This will basically leave them to make sure their code isn't doing anything really magical.

    0 讨论(0)
  • 2021-02-08 08:13

    Thanks for the suggestions all. In the end I think Kent's explanation was the closest, which basically amounted to "Prototype is broken". (Sorry if I'm summarizing you incorrectly :)

    As for the jQuery.noConflict option - I already mentioned this in the question. It makes a difference when you run this method, and I have very little control over that. As I said, I have tried running it in a couple of different places (specifically the page header and also from my script file), to no effect. So, much as we'd all like it to be, "just use noConflict" is not an answer to this question, at least not without additional information.

    Besides, jQuery.noConflict seems to be about the $ variable, and the code around the error point does not deal with that variable at all. Of course they could be related indirectly, I haven't tracked it down.

    So basically I ended up rewriting the script using jQuery instead of Prototype, which actually had its own problems. Anyway I've published the whole war story on my blog, should you be interested.

    0 讨论(0)
  • 2021-02-08 08:14

    I think you should search well because all jQuery plugins has a prototype version and all prototype plugins has a jQuery version. If you really don't find what you look and you can't use only one library, take a look here at

    jQuery.noConflict();
    

    But again, i think it make no sense to load over 15-20kb for each library :)

    0 讨论(0)
提交回复
热议问题