Skip to content

Events are fired without arguments and event listeners are ignored #68

@DerGernTod

Description

@DerGernTod

This code simply executes xhr.onreadystatechange, xhr.onload and xhr.onloadend. There's a lot of different ways how events can be registered and fired (e.g. if one adds listeners with xhr.addEventListener(...). This means that if you don't register your listeners with one of the three shorthands, they are never fired.

Also, the mentioned code calls the callbacks without event arguments:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readystatechange_event
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/loadend_event
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/load_event

This leads to consumers not being able to access these events in their registered callbacks, which leads to exceptions if they do so. A very simple reproducer:

const x = new XMLHttpRequest();
x.open("GET", location.href);
x.addEventListener("progress", (event) => {
  console.log(`progress: ${event.loaded * 100/event.total} %`);
});
x.addEventListener("load", (event) => {
  console.log(`loaded ${event.loaded} bytes`);
});
x.send();

In this scenario, none of the events are fired. if you replace the addEventListener calls with the shorthands, you can see that only load is called without the event argument (progress is not called whatsoever):

x.onprogress =  (event) => {
  console.log(`progress: ${event.loaded * 100/event.total} %`);
};
x.onload = (event) => {
  console.log(`loaded ${event.loaded} bytes`);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions