jQuery and Windows 8 JavaScript Metro Style Apps - javascript

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.

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.

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

How to use a contentScriptFile from an external URI in a Firefox addon

My latest update to a Firefox addon has been rejected because I've used a custom jquery-ui (generated by their site with just the widgets I wanted) and it fails their checksum check.
Your add-on includes a JavaScript library file that doesn't match our
checksums for known release versions. We require all add-ons to use
unmodified release versions, obtained directly from the developer's
website.
We accept JQuery/JQuery-UI libraries downloaded from
'ajax.googleapis.com', 'jquery.com' or 'jqueryui.com'; and used
without any modification. (file-name change does not matter) I'm
sorry, but we cannot accept modified, re-configured or customized
libraries.
Fair enough, I could just download the full one and resubmit, but I was wondering if it is possible to link to one instead?
If I try this:
contentScriptFile: [self.data.url("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"), self.data.url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"), self.data.url("api.js")],
I get an error at runtime telling me that content scripts much be local. Both google and the API seem to be proving illusive to me for an answer.
Does anyone know if this is possible and how?
Cheers
Rich
self.data.url("https://...")
It seems like you haven't read the documentation on data.url()
It clearly states that
The data.url() method returns a resource:// url that points at an embedded data file.
Which means you cannot link to an external resource.
Does anyone know if this is possible and how?
No, contentScriptFile runs with (slightly) elevated privileges compared to regular web content, that's why you are not allowed to load scripts from sources that might change and could theoretically inject malicious code in the future.
If you want to rely on external libraries and keep them up to date you could simply write a little build script that always downloads the newest version when building a new XPI.
In principle you could just load the script via privileged XHR and then pass it as string, but that's probably not gonna pass AMO review.
And piece of personal opinion: Since you're targeting a specific browser you don't really need jquery for its cross-browser logic, modern web APIs provide lots of convenience methods that you can get pretty far just with vanilla ES6-javascript and state-of-the-art DOM APIs. Iterators and arrow functions also make bulk operations fairly concise.

how do i avoid including jquery twice?

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

Is there a lightweight library to do draggable+sortable lists inside greasemonkey scripts?

I'm writing a GreaseMonkey script, as part of which I'd like to sort a list by dragging its items. I'm using mootools, but the component for sortable lists doesn't work in the GM sandboxed environment. Can you recommend a smallish library/piece of code to do list sorting in the most lightweight fashion? I want it to be independant of any large framework and don't feel like implementing it myself.
If you need to debug a library such as mootools, instead of #require-ing it, just copy/paste the entire source into your own script. Then you can debug it the same as your own code.
On another note, this script includes the ability to drag-and-drop it's own window around the screen, with no additional library. Maybe you could analyze it and borrow code?
Use this Javascript QuickSort implementation
I'm not sure if there are any particular issues with just MooTools, but in general you may include external scripts into a GreaseMonkey script.
This has been addressed in previous questions like this one or this, as well as on the GreaseSpot wiki.

Categories