How to use different version of JQuery on a JS fil

2020-02-07 10:13发布

I have a Cisco JS library designed to work using JQuery 1.4.2 and I'm using latest 2.X version of JQuery on the UI page I'm developing.

Cisco Library

I'm using Jabberwerx.js file from the above library link.

The library works fine if the JQuery loaded is of 1.4 but fails to work with later versions of JQuery. If I use old version of Jquery my UI based on bootstrap doesn't work. I tried to use noConflict() but then I can not edit the entire library which is very huge. There are lots of webservice calls and functions on the library so upgrading it is very painstaking.

Is it possible to use a particular version of JQuery on this JS file and the rest of the application can use the latest version of JQuery?

The library has this code on it. Can we change this to make it work using old version of JQ.

(function(window)
{   var jQuery=function(selector,context)
    {
        return new jQuery.fn.init(selector,context);
    },
    _jQuery=window.jQuery,
    _$=window.$,
}

I'm not looking at using different versions of JQuery on the same page but I'm trying to limit the usage of one version of JQuery to one of the JS files.

2条回答
Deceive 欺骗
2楼-- · 2020-02-07 11:02

When you use different version of jquery then your code conflicts. So there may appear obvious errors.

To fix it, you need to use $.noConflict() passing a boolean parameter true.

jQuery.noConflict(true);
//removes jquery itself and allows you to work with different version of jquery

Example:

<script src="jquery-version-1.0"></script>
<script>//code for v-1</script>
<script>jQuery.noConflict(true);//remove jquery</script>
<script src="jquery-version-2.0"></script><!--use another version-->
<script>//code for v-2</script>
查看更多
相关推荐>>
3楼-- · 2020-02-07 11:09

In addition to what Bhojendra Nepal's answer, You can also check the version of jQuery through the following Code :

var jq_version = $().jquery;
    console.log(jq_version);
    /* gives the version number like 1.7.1. So may be you can write an if condition around the version number. For e.g :*/
    if(jq_version == '1.7.1'){
        //do something for ver 1.7.1
    }

There are even more ways to Check the version of jQuery. But I'm not sure if that would help much with the CISCO library you are using.

查看更多
登录 后发表回答