How to use different version of JQuery on a JS file? - javascript

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.

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>

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.

Related

How to hide the version of jQuery

I'd like to hide the version of jQuery library used in my project so nobody can see which version is in use (at least just by quick looking at the code).
While removing a JS comment containing jQuery version is fairly easy, there is a variable hardcoded into jQuery which reveals the version:
var ah = "1.11.3"
My wish is to empty the variable, however I'm not a frontened developer so I don't know what could potential consequencies.
Does jQuery uses this variable is some particular way?
So two points:
Yes removing that could have issues, 3rd party plugins will use: jQuery().jquery to get the version to see if the version used is a recent enough version for the plugin to work, if you remove that the plugin could not load or try and work and have massive issues if the version of jquery isn't 'good' enough.
Removing the version won't help in people not being able to identify if they really want to, it just adds a 2 minute step to the process of just comparing the rest of the code to jquery versions.
I would strongly advise not to remove the version number or change it.
I agree with others point but to answer your question . You can remove the version. you can just empty the version string.
var version = " "
If you give this in your source code even if you try to get the version using $.fn.jquery it gives the empty string.
To give more insight:
download the jquery from jquery.com
save it in the same folder where you create your file.
Include the script in your file in the script src tag.
go to the downloaded file and empty the version variable( var version = " ")
Now in your file even if you try to get the version number ,you cant.
Note : Try to not use the vulnerable version.

Error: SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js [duplicate]

I use SignalR 2.2.0 in a MVC5 project. SignalR depends of JQuery in client-side.
JQuery recently released new version, I updated it from Nuget, specifically from version 2.2.4 to 3.0.0.1, but then SignalR stopped working. Startup command "$.connection.hub" in javascript fails.
After a long time head scratching, I downgrade JQuery to 2.2.4 and everything is fine again.
Am I the only one getting this problem? There is any workaround?
Thanks.
You must edit the signalR code by yourself, In jquery 3 they removed the shortcut for load event :
Breaking change: .load(), .unload(), and .error() removed
These methods are shortcuts for event operations, but had several API
limitations. The event .load() method conflicted with the ajax .load()
method. The .error() method could not be used with window.onerror
because of the way the DOM method is defined. If you need to attach
events by these names, use the .on() method, e.g. change
$("img").load(fn) to $(img).on("load", fn).
https://jquery.com/upgrade-guide/3.0/
so in the file jquery.signalR-{version}.js :
you must update this line :
_pageWindow.load(function () { _pageLoaded = true; });
To :
_pageWindow.on("load",function () { _pageLoaded = true; });
Finally version 2.2.1 of SignalR was released, solving this problem. Thanks for all comments.
If you're still getting errors like this after updating to 2.2.1 and jQuery 3.x then read on...
TypeError: Cannot read property 'client' of undefined
Like I am you are probably using the dynamically generated proxy, and you checked your /signalr/hubs file and found you don't have any proxies defined.
var proxies = {};
Wait you may ask I didn't change anything - where did they go?
Well, like me you probably were in such a hurry to upgrade signalR to 2.2.1 that you forgot to do it in all your projects and now you are using both 2.2.1 and 2.2.0 in different assemblies. (I am defining my hubs in a different assembly than my main app).
All I needed to do was make sure I had the latest nuget package version in every project and it all worked. Should work fine after rebuilding. If not, this may also help.
Also do yourself a favor and read the jQuery 3 upgrade guide if you use much jQuery elsewhere.

How to add jQuery file to navigation website?

I am working with this demo: http://cssdeck.com/labs/setting-active-states-on-sticky-navigations-while-scrolling/
For some reason when I try to recreate it the Java part making the navigation work isn't coming through on my version here: http://cssdeck.com/labs/ydsxavxy
So far I saved out index.html, style.css, and flip.js and attempted to link the CSS and JS in the index.
How can I get my version working like the demo? How is a Java file included outside of CSSDeck? I am not too familiar with how CSSDeck takes the HTML, CSS, and Java and combines them into one page preview.
The version of jQuery you are loading is not correct.
The example that works uses 1.8, and you are loading 1.4:
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
You can easily know this by looking at your browser console and seeing what errors it's throwing. In your case, the error is as follows:
TypeError: $(...).on is not a function
This usually means your jQuery library is not defined or not supported by the code you're using.

Unit Testing dynamic loading of javascript libraries

I am writing JavaScript tests in Jasmine for a method that dynamically load jQuery if not present or the version loaded is an earlier version that what the plugin requires. What is needed is to test a couple of different scenarios.
No library exists
An earlier version exists, so is upgraded
A new version is specified (options)
etc
The issue is that the test adds a version of jQuery to the page, so this effects/interferes with later tests. Is there any way to reload the page or remove added javascript?
Solution
I've found a work around for testing. The code to check if JQuery is loaded is:
if(typeof jQuery == 'undefined'){ [load JQuery]; }
In the Jasmin afterEach method:
jQuery = undefined;
It doesn't actually remove the dynamically loaded library, but the method being tested doesn't see it.

Google Libraries - URL version selecting

Reading through documentation,
I found following:
1.9.1
1.8.4
1.8.2
A version of "1.8.2" select the
obvious, fully-specified version.
Specifying a version of "1.8" would
select 1.8.4 since this is the highest
version released in the 1.8 branch.
For much the same reason, a request
for "1" loads version 1.9.1, since
this is the highest version released
in the 1 branch. Note, these
versioning semantics work the same way
when using google.load and when using
direct script urls.
Does that mean, that I can use something like
http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
and I get latest jQuery v1.** ?
Or, would it be even possible to include:
http://ajax.googleapis.com/ajax/libs/jquery/jquery.min.js
Well, just testing the two URL you gave :
http://ajax.googleapis.com/ajax/libs/jquery/jquery.min.js
This doesn't work : I get a 404 error.
So, it seems you must specify at least the major version number (i.e. the first digit in 1.4.2) -- which seems reasonable, as a change in major version number generally means a big change in the API (which will probably require you to change stuff in your application).
And with the first one :
http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
I get jQuery 1.4.2 -- which is indeed the most recent version.
And if I test with prototype :
http://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js
I get the most recent version too.

Categories