how do i avoid including jquery twice? - javascript

i have a service i'm building, which will be included as JS on client web pages. optimally, i want to build the service using jquery.
the case may arise, where the page already has JQ included.
how do i avoid including JQ twice in the page? how do i make sure i still have the desired JQ functionality, even if the page has already included an older version of JQ?
is the only solution simply abandoning JQ and going with hand-coded script?

Check for the existence of jQuery and match the version you need against jQuery.fn.jquery.

from the linkedin jquery group:
Ever want to use the latest jQuery, but old code or components leaves you with an older version? Well this standalone version can be included anywhere on the page and will take a custom name space of jQuery14, and will not affect jQuery or $ in any way. Also, easy way to integrate among other frameworks.
http://www.matthewdunham.com/jquery-1.4.2.min.standalone.js

Related

What happens if i don't use jquery CDN slim package of bootstrap v4

What happens if I don't use jQuery slim package that comes with Bootstrap v4 and instead of that I use the jQuery host CDN available on Jquery site. I google it and I know there is a difference between these two as the slim version does not have various functions like ajax or animations. Does it effect anything on my website if I remove the slim jQuery package that comes with Bootstrap v4.
I am just a beginner not a pro like others but I want to add some features on a page with Ajax. I spent hours online and try many codes but nothing works. In last I delete one by one script of Bootstrap than I found there is something with jQuery CDN link. When I used google host jQuery CDN the code works but not with the slim Bootstrap jQuery.
Does it effect anything on my website if I remove the slim jQuery package that comes with Bootstrap v4?
So long as you have an up-to-date version of jQuery, then nothing will be affected from the point of view of bootstrap. The slim version that Bootstrap comes with simply provides functionality for the various features of Bootstrap that rely on jQuery to work properly like collapsible navbars, alerts, etc.
You can absolutely use a separate jQuery, and honestly probably should. jQuery is a fantastic library that was designed to make JS cross-browser compatible and making use of it drastically improves your code.
Also, I would recommend not using a CDN if possible. CDNs are great for speedy development but if your website is going to go into production and be used for any length of time, it's better to have dependencies like Bootstrap and jQuery locally hosted because CDNs can and will go down for any number of reasons (server shutdown, server migration, DDoS, perhaps the person accessing your website's internet access is limited and won't allow them to make GET requests to that CDN, etc.) and if any of those reasons occur, you won't be alerted. Your website will just break.
Edit: So I do want to make an update. Nowadays it seems like using a CDN in production has become more common thanks to the reliability of CDNs and the benefits of global CDN networks having nodes (likely) closer to your customer being comparatively quicker to serve the content than requests against your webserver.
The issue with the slim version of jQuery is losing all AJAX functionality.
I have replaced the default jQuery slim reference from Bootstrap 4 with the normal version:
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
Found here https://code.jquery.com/
Slim version(jquery-3.2.1.slim.js) of bootstrap's Jquery is actually a lighter version 3.2.1/jquery.js
In this slim version ajax related code has been removed so you have no ajax support with this .slim.js
Just check the first line of Jquery-3.2.1.slim.js
jQuery JavaScript Library v3.2.1 -ajax,-ajax/jsonp,-ajax/load,-ajax/parseXML,-ajax/script,-ajax/var/location,-ajax/var/nonce,-ajax/var/rquery,-ajax/xhr,-manipulation/_evalUrl,-event/ajax,-effects,-effects/Tween,-effects/animatedSelector
This is jQuery JavaScript Library v3.2.1 without -ajax,-ajax/jsonp.....-effects/animatedSelector
So 3.2.1/jquery.js is the superset of jquery-3.2.1.slim.js
So jquery.js is recommended if you are using any of removed methods(in slim.js) in your project otherwise you can use jquery-3.2.1.slim.js directly.

jQuery dependency on asp.net mvc site

I have an Asp.NET MVC 4.0 site that uses jQuery 1.11.1. I had an issue where some of the javascript on an end users browser (IE) was not working. For some reason, they had version 1.7.x of jQuery. I've been coding for a long time, but I'm relatively new in the web development area.
Why would the browser not download my copy of jQuery from the server? Is there a way to force the browser to get my version?
Thanks!
If the client is loading your site normally (calling an endpoint on your server) then there is no way they could load any version of jquery other than the one you have included in the script tag on your web page.
If you are doing something fancy like providing a widget that your user embeds in a page then indeed, depending on the order in which the scripts get loaded, the browser might wind up with a different version of jquery.
If you're doing the latter there are methods you can use to get a particular version of jquery for your code. See here for a start:
Include a specific version of JQuery and a plugin without conflicting with the page's JavaScript library?
Based on the comments below, you might also have other libraries in your application that also load jquery. The $ variable will get the last version of jquery to load. I would have thought that every browser would load them in the same order, but perhaps on older copies IE things happen differently (or perhaps there are paths through your app that load things in a different order).
If any of that's true then you'll have to use one of the techniques above. I'd also look into whether you can force kendo not to take over $.

Custom building jQuery

I'm using jQuery for a mobile app, but I don't use all or even half of its modules so I'm looking to nix those and reduce the size a bit. (No, I don't want to use Zepto or other libraries for some reasons).
There was one: projects.jga.me/jquery-builder/ but its not maintained anymore.
jQuery Uncompressed: http://pastebin.com/CwUT9U8p
Wondering which functions/modules I can safely remove, any pointers would help.
you can use grunt to build the jquery modules you want to use into a custom version of jquery, it's documented on the jquery git repo here
It's worth noting though that jquery uses a lot of internal code calls, so even a trimmed down version wont be as small as you might like (possibliy not even as small as a minified gzip version of the full lib)
If you are only wanting sizzle and getJson have you though of using the stand alone sizzle lib and a lightweight json ajax wrapper? there are microlibs for ajax/json here

Writing a jQuery library with dependencies?

I'm writing a jQuery library that has a couple jQuery library dependencies, one being jQuery Cookie. What's the best way to include this so that the users of my library use it?
I can think of at least three:
1 - don't use jQuery Cookie, and instead include my own cookie utility functions
2 - include jQuery Cookie in my code, and use it unless it is already present on the page
3 - include jQuery Cookie as a separate file, and state that the users have to include jQuery Cookie to use my library
What's the preferred method?
If what you're doing with the cookies isn't overly complex, you should probably write your own small helper function to achieve that. This would be the best option.
If you have to use the jQuery Cookie plugin, do not include it in your script. State it as a dependancy, and link to the jQuery cookie page. There many reasons for not including it in your plugin, but here are 2 that come to mind:
If the user already has that plugin loaded, including it again in your script will result in many extra bytes wasted for no good reason.
When the original author of the other plugin subsequently issues bugfixes to their code, you don't want to constantly keep track of that and issue updates to your plugin.
I'll suggest go with #2 or #3.
Since your user of the library are developers, you can just mention in your documentation on which other library and their versions are needed.
Do a failsafe, try-catch checking first if the other libraries are available so your own library won't throw some errors.

jQuery and Windows 8 JavaScript Metro Style Apps

Can jQuery be used in Windows 8 Metro-style apps developed using the JavaScript API? I'm looking at the samples, and there's a lot of standard DOM manipulation like document.getElementById, addEventListener, etc. I'd like to use jQuery for productivity.
The $ is not magic. jQuery is just a javascript library. It should run fine.
The only thing that might not work are cssHooks which won't take into account any 'quirks' of their environment.
So yes, it will work, if you trust Microsoft to produce an environment that follows the HTML/CSS/JS specifications.
Do you feel lucky?
Yes, it works, as will any JavaScript library. I verified it on one of the tablets they gave out at the BUILD conference.
However, do not expect to use a CDN to load in the script. You have to download it, add it to the project, and reference it locally.
tl;dr : you can use jQuery 1.7+, but it's not (yet?) fully integrated when markup modification is involved.
Quote from the Dev Center (msdn)
Using jQuery
You can use jQuery in your Windows Store app using JavaScript, but only versions 1.7 and later. We recommend always using the latest version.
That means that it will work, but you will encounter some non-standard behaviors as listed in the HTML and DOM API changes list (msdn)
There are exceptions mentioned when setting the innerHtml property (among others : Making HTML safer) if there is unsafe markup, but this is non-blocking and the simple fact of loading the jQuery library (1.8.2) will trigger a few.
As mentioned before, you do need to use a local copy of the library (no CDN).
You can use any JS library. I suspect that methods like WinJS.xhr (aka '$.ajax') where written so that WinJS doesnt have a dependency on jQuery.
It should run fine. jQuery is just a library. The $('#someid') will internally call the document.getElementById You can use it just as if you can use the javascript code you write yourself...
JQuery should work fine but bear in mind there is some built in functionality for doing this kind of thing, in the case of your example:
WinJS.Utilities.query("#someId li")
.listen("click", function (result) {
Query Collection Documentation
Don't use any windows libraries like WinJS. Why would you want to lock yourself and be dependent on proprietary code? Stick with the usual stuff, i.e. HTML, CSS, JS(JQuery or otherwise). It will only make your future support/dev far more predictable ...
As with all third party JavaScript libraries there are a couple of things you should keep in mind.
Always try to include the .js sourcefile in your project when possible. Calling out to a CDN or external hosted location will flag you for some security checks you wouldn't normally have to go through.
I have used a couple of jQuery versions in coding my own apps. Some of them broke so keep in mind there is still no guarantee. Other libraries, like SoundJS, I used older versions that I know do work.
If you are looking for some examples of third party libraries I have some posts on Knockout and Upshot here:
http://blogs.msdn.com/b/davedev/archive/2012/05/02/connecting-to-wcf-ria-services-in-a-windows-8-metro-style-app-using-upshot-js-and-kockout-js.aspx
I have also written a free Windows Store Gaming Kit that uses third party libraries you can grab the free source for that here: http://win8gamekit.codeplex.com
Actually you can download it through Visual Studio. If you download NuGet Package Manager.
TOOLS->Add Packages & Extensions
Search the left online packages for NuGet Package Manager and install.
Then when in a project that you want jQuery, right click on your references and add a NuGet package, and search jQuery. That's what I use.

Categories