Window.history.back illegal call

Why can I do this:

$("button").on('click', function(){window.history.back();});

however, when I try;

$("button").on('click', window.history.back);
/*or*/ $("button").on('click', history.back);

I get:

Uncaught TypeError: illegal call
in HTMLAnchorElement.dispatch (jquery-1.12.4.js: 5226)
in HTMLAnchorElement.elemData.handle (jquery-1.12.4.js: 4878)

I got the impression that when there is no saved context, it defaults to the window object, which will allow me to do this?

+4
source share
3 answers

First of all, it history.back()should be called using historyas a context, if it is equal by default window, it will throw the error you described. The problem here is that jQuery calls a handler with an element that has an event binding.

, , . , jQuery , , history.back() .

+3

history.back():

... window.history.back(); ...

back() history, , back this history.

back:

... window.history.back ...

jQuery .on().

jQuery click, this . jQuery this DOM, .

, back() this, , history, .

, , , , , .

+2

The attribute history windowsimply refers to the History interface and can only be called globally.

The failure method uses the context of an element that does not have an attribute history.

0
source

Source: https://habr.com/ru/post/1687986/


All Articles