0

The jquery v2~ supports load function:

$(selector).load(function () {}).error(function(){});

The jquery v3~ doesn't support load function and we need to use the on function:

$(selector).on('load', function () {});

How can I handle errors with the on function?

2
  • You can take a look at this. Commented Sep 21, 2016 at 13:50
  • @Bla... This post is only about silent errors in async ready states. That is not the question here. Your case is very special. ;) Commented Sep 21, 2016 at 13:58

1 Answer 1

2

Just to be sure, jQuery 3 still supports .load() to load content. Only the event listener creation and trigger needs to use .on() and .trigger().

// jQuery 2
$("selector").load(function() {
    console.log("I'm loaded!");
});
$("selector").load();

// jQuery 2 + jQuery 3
$("selector").on("load", function() {
    console.log("I'm loaded!");
});
$("selector").trigger("load");

The error handling is the same. .error() is deprected, use on again:

// jQuery 2 + jQuery 3
$("selector").on("load", function() {
    console.log("I'm loaded!");
}).on("error", function() {
    console.log("I'm having errors!");
});
$("selector").trigger("error");

Or combine both event listeners in a single .on():

// jQuery 2 + jQuery 3
$("selector").on({
    load: function() {
        console.log("I'm loaded!");
    },
    error: function() {
        console.log("I'm having errors!");
    }
});
$("selector").trigger("load");
$("selector").trigger("error");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.