0

Wondering how this jQuery script could/should be updated to be compliant with jQuery v.3+? This script adds & removes a class from our main content wrapper, with which we use CSS opacity & transition to create elegant fade-out and fade-in on page loads and transitions.

Has worked for years, but WordPress updated jQuery from 1.12.x to 3.5.x and now it totally randomly doesn't remove class on load in Firefox (other browsers don't seem to struggle). I can't figure out a pattern to when it will happen, it's just random and on different pages. This leaves page content invisible (opacity: 0 = not ideal).

Error we get is: "jQuery(window).on('load'...) called after load event occurred"...

jQuery( document ).ready(function( $ ) {
// All of our other functions... 

 // Remove .fade-out class from content wrapper after page loads
 $(window).on('load', function () {
    $("#content-wrap").removeClass("fade-out");
});  

window.addEventListener("pageshow", function() {
    $("#content-wrap").removeClass("fade-out");
}, false);


// Add .fade-out class to content before page unloads
window.addEventListener("beforeunload", function () {
  $("#content-wrap").addClass("fade-out");
});

});

================

Here is my attempt at a fix based on the initial comments (hope this is right way to present this):

(function( $ ) {
// Remove .fade-out class from content wrap after page loads
 $(window).on("load", function () {
    $("#content-wrap").removeClass("fade-out");
});  

window.addEventListener("pageshow", function() {
    $("#content-wrap").removeClass("fade-out");
}, false);
}( jQuery ));

jQuery( document ).ready(function( $ ) {
// All of our other functions... 

// Add .fade-out class to content before page unloads
window.addEventListener("beforeunload", function () {
  $("#content-wrap").addClass("fade-out");
});

});

Additional Questions About Fix:

  • Is this proper syntax (sorry, not a JS expert yet)?

  • Would this solution present possibility of .fade-out being removed BEFORE page contents (e.g., images) complete loading? It is a bit less elegant to have the page fade in and then additional images come popping in.

  • Is there any known reason why the original code works fine on jQuery 1.x but fails occasionally with jQuery 3.x? And only fails on Firefox??

Kenny J
  • 11
  • 1
  • 10
  • 1
    The real problem, as the error message is trying to tell you, is that the event listener is being added *after* the event has already occurred. So apparently the code that has those lines is loaded after the load event has already occurred, possibly due to the script being loaded with "async" or "defer" such that it is no longer guaranteed to be loaded before window.onload. – Heretic Monkey Mar 16 '21 at 02:51
  • @HereticMonkey is right. It does not make sense to include `$(window).on('load')` inside `jQuery(document).ready`. Read [this](https://stackoverflow.com/questions/3698200/window-onload-vs-document-ready) – Raptor Mar 16 '21 at 03:02
  • I took a stab at modifying code based on suggestions. In testing it 'mostly' works, I believe. What is proper S.O. etiquette for posting revision for review - edit orig question or post my own 'answer'? – Kenny J Mar 23 '21 at 00:06

0 Answers0