eloism eloism - 15 days ago 5
Javascript Question

Jquery update to 3.1.1 -> Anonymous functions

To implement a library I had to upgrade JQuery to its 3.1.1 version.

It went pretty well despite a few deprecated functions easily fixed with some researches,

but one small script I am using on my website isn't working anymore and my JS knowledges are not that good.

The script in question is CountrySelect (it displays country names/flags inside an input list), here are the errors I am getting in my console :


r.fn.load @ jquery-3.1.1.min.js:4

(anonymous function) @ countrySelect.js:32

(anonymous function) @ countrySelect.js:8

(anonymous function) @ countrySelect.js:10


and here are the 34 first lines of the script :

// wrap in UMD - see https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
(function(factory) {
if (typeof define === "function" && define.amd) {
define([ "jquery" ], function($) {
factory($, window, document);
});
} else {
factory(jQuery, window, document); // <------ LINE 8
}
})(function($, window, document, undefined) { // <------ LINE 10
"use strict";
var pluginName = "countrySelect", id = 1, // give each instance its own ID for namespaced event handling
defaults = {
// Default country
defaultCountry: "",
// Position the selected flag inside or outside of the input
defaultStyling: "inside",
// Display only these countries
onlyCountries: [],
// The countries at the top of the list. Defaults to United States and United Kingdom
preferredCountries: [ "fr", "us", "gb", "ru", "cn" ]
}, keys = {
UP: 38,
DOWN: 40,
ENTER: 13,
ESC: 27,
PLUS: 43,
A: 65,
Z: 90
}, windowLoaded = false;
// keep track of if the window.load event has fired as impossible to check after the fact
$(window).load(function() { // <------ LINE 32
windowLoaded = true;
});


I would really appreciate some help to understand why it is not working anymore, I can't figure it out so far. How should I fix this ?

Answer

The .load() method has been removed in jQuery 3. replace line 32 with the following code:

$(window).on('load', function() {  // <------ LINE 32
  windowLoaded = true;
});

Source: https://jquery.com/upgrade-guide/3.0/#breaking-change-load-unload-and-error-removed

Comments