开发者

jQuery UI and Prototype conflict

I am adding a new form to our website in Perl (wasn't my choi开发者_高级运维ce). There is a lot of html generate automatically for the forms to create the consistant look. My problem comes in where the legacies use prototype for various things throughout the page (including on load). However i want to use jQuery and mainly the date picker from jQuery UI. I can avoid conflicts by using jQuery.noConflict();. But i still get an error becuase of a line of code in the jQuery date picker javascript.

inst.dpDiv.zIndex($(input).zIndex()+1);

See how it still contains the $ symbol, which prototype tries to handle but can't. Does anyone have any solutions for me? I am using jQuery 1.5 and jQuery UI 1.8.6.

In summary: I can't remove prototype.js, I would prefer to use jQuery UI datepicker and jQuery UI datepicker doesn't handle jQuery.noConflict().

Thanks

EDIT This only happens when i try to click on the button to show the date picker. From firebug:

$(input).zIndex is not a function
inst.dpDiv.zIndex($(input).zIndex()+1);
datepicker.js (line 651)

EDIT EDIT Updating to jQuery UI 1.8.9 doesn't change the problem.

EDIT EDIT EDIT This problem occurs when clicking the button to show the date picker. So on load the datepicker is set up fine. Changing the order of the scripts doesn't work, and any sort of variation of the following code doesn't work either.

(function($) { 
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);


<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    jQuery.noConflict();
    jQuery(function ($) {
        // your code here
        // $ is the jQuery object
    });
</script>

jQuery will pass itself to the inner function. Just specify $ as the parameter and $ inside the function will refer to the jQuery object. The jQuery UI datepicker does the same. Make sure you include the libraries in the above order, Prototype first.


I dont think there are any elegant solutions. I think you have two options - and they both involve search/replace which shouldn't be a big deal if you're proficient with perl.

  1. replace the $-function with jquery() in the datepicker library and add a jQuery.noConflict()

  2. replace the $-function in prototype with $$$ (or another name) and all it's instances

The second option may be your best bet if you are in the process of removing prototype. It is less obtrusive - but then again it depends on the amount of code you have in prototype. I'd be interested to see if anyone has any other ideas.


Thanks for all the responses. I couldn't end up figuring out how to fix the conflict. So i removed the jQuery UI datepicker from the form. Thanks again.


Had the same issue. Just load the jquery library AFTER prototype and then add nOConflict. Example using prototype and jquery:

<script src="prototype.js" type="text/javascript"></script>
        <script src="scriptaculous.js" type="text/javascript"></script>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
        <script>

        $.noConflict();
        jQuery(document).ready(function($) {
            $( ".date" ).datepicker({
                        dateFormat: "dd-mm-yy",
                        altFormat: "yy-mm-dd 00:00:00",
                        altField: "#date",
            });
        });


I had an issue where I was loading in jQuery library, but dynamically loading prototype.js; when I came to use jQuery.noConflict(), it wasn't fixing the issues I was having. $.noConflict() was also not working as prototype.js does not know what to do with this, so the fix I found to work was:

$ = jQuery;

This effectively reassigns the $ back to jQuery.

Hope it saves some of you the pain I went through in making this simple discovery.


I don't think your problem arises from a library conflict. I had the same problem and I was only running jQuery and not any other libraries. The datepicker requires the jQuery UI core JS file to run (written in the datepicker file header). Make sure you add that before the datepicker file and everything should be fine.

You'll obviously still need the jQuery.noConflict() to ensure the two libraries don't conflict.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜