I'm trying to use the latest version of the timepicker addon for jQuery UI.
Another library already loaded it at an earlier version (1.0.4
) but I want to use the latest (1.4.5
) version.
In the source, the plugin checks to see if it has already been loaded before proceeding:
$.ui.timepicker = $.ui.timepicker || {};
if ($.ui.timepicker.version) {
return;
}
I did a lot of searching for ways to do this but didn't come up with anything helpful. I did see that it might be possible to load multiple versions of jQuery on the page using noConflict
and then attach it to a different version of jQuery but I thought there should be an easier way.
I gave this a try it is not working - I think because the variables are getting referenced and they maybe need to be instantiated instead - I'm not a JS expert but at least I'm trying:
// jQuery, jQueryUi, and the 1.0.4 version loaded beforehand
<script>
var _old_jquery_ui_timepicker = jQuery.ui.timepicker;
var _old_jquery_ui_timepicker_function = jQuery.fn.datetimepicker;
jQuery.ui.timepicker = null;
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.4.5/jquery-ui-timepicker-addon.min.js"></script>
<script>
jQuery.ui.mytimepicker = jQuery.ui.timepicker;
jQuery.ui.timepicker = _old_jquery_ui_timepicker;
jQuery.fn.mydatetimepicker = jQuery.fn.datetimepicker;
jQuery.fn.datetimepicker = _old_jquery_ui_timepicker_function;
</script>
Writing that code felt dirty, and again, it didn't work. So, after not having any luck with that I tried searching more and still didn't come up with anything. I tried searching for:
- loading multiple jQuery plugin versions on the same page
- jQuery plugins noConflict
- load a jQuery plugins in a different namespace
- load a jQuery plugins in a different domain
- load a jQuery plugins in a different context
Is this simple to do and I'm just not searching or the right thing or is it impossible?
Why I'm attempting this
I need features from the newer version of the the timepicker, but I don't want my code to break when the other library is updated or replaced.
- The web app is using a (non-jquery) plugin with a large codebase that is reliant on the earlier version (
1.0.4
) of the timepicker addon - It's not my place to update the other plugin's codebase
- I don't want to write code that is reliant on the earlier version (
1.0.4
) of the timepicker addon, as would be reliant on the other codebase to load the older version (and would possibly break when that was updated)
So, I agree that using two versions of a jQuery plugin is not ideal. However, I do need to add in functionality from a later version of the jQuery timepicker plugin. I believe it is possible to do this, maybe with Javascript's prototyping and inheritance, and without having to 'hack it' as @Oliboy50 suggests doing with a find-and-replace to the plugin's sourcecode.
Are there any Javascript pros who could demonstrate how to do this in (even though it is not ideal)?
Please note: yes, we have established that "it is a bad idea" to try and do this, but for principle I would like to know if Javascript's prototypal language would allow this to be done. noconflict
(i.e. loading multiple versions of jQuery) was already mentioned in comments and answers. Although this "would work" it's not the answer I'm looking for. Also, modifying the plugin sourcecode with a find-and-replace is not really an elegant solution either.