admin管理员组文章数量:1359424
Why is it that I can 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 invocation
at HTMLAnchorElement.dispatch (jquery-1.12.4.js:5226)
at HTMLAnchorElement.elemData.handle (jquery-1.12.4.js:4878)
I was under the impression that when there is no retained context it defaults to the window object, which would allow me to do this?
Why is it that I can 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 invocation
at HTMLAnchorElement.dispatch (jquery-1.12.4.js:5226)
at HTMLAnchorElement.elemData.handle (jquery-1.12.4.js:4878)
I was under the impression that when there is no retained context it defaults to the window object, which would allow me to do this?
Share Improve this question asked Oct 22, 2017 at 21:41 ZzeZze 18.9k14 gold badges94 silver badges125 bronze badges 12- 1 jQuery expects function as second paramer – Kresimir Pendic Commented Oct 22, 2017 at 21:44
-
1
history.back()
needs to be invoked withhistory
as the context, notwindow
. You'll get the same error if you try to runwindow.history.back.call(window)
. Regardless, jQuery will invoke the function with the element as context, so it doesn't even default towindow
– Lennholm Commented Oct 22, 2017 at 21:44 -
1
like @MikaelLennholm says, back is part of history, not window. so you would need to do
history.back.bind(history)
– Keith Commented Oct 22, 2017 at 21:47 -
1
@Zze No, it wouldn't work either because
addEventListener
also sets the element as the context for the handler function. – Lennholm Commented Oct 22, 2017 at 21:59 -
1
I strongly remend the approach you used in the first example in your question. Sure, you can use
history.back.bind(history)
instead, but there is no advantage to that. Your first example with the anonymous callback function that makes the call tohistory.back()
is basically the canonical way to do this. It is much more clear than the.bind()
version. Why make things more plicated than they need to be? The only improvement I would suggest to the first version is to split it into multiple lines in the usual manner so it's not so crowded. – Michael Geary Commented Oct 23, 2017 at 4:00
3 Answers
Reset to default 5First of all, history.back()
must be invoked with history
as the context, if it defaults to window
it will throw the error you describe. The issue here though is that jQuery invokes the handler with the element that has the event listener bound.
Your first line of code works because context is irrelevant to the anonymous function you pass as a handler. The fact that jQuery sets the element as the context doesn't matter since you correctly invoke history.back()
inside of it.
Your working code calls history.back()
:
... window.history.back(); ...
That calls back()
as a method of the history
object, which simply means that inside the back
function, this
is a reference to history
.
The non-working code gets a reference to the back
function:
... window.history.back ...
and then passes the reference into jQuery's .on()
method.
jQuery calls the function later in response to a click
event, and this
is set at the time of that call. jQuery sets this
to the DOM element receiving the event.
So when the implementation of the back()
function references this
expecting it to be the history
object, it gets the wrong thing.
In general, when you want an event listener to call a function that you didn't write, you're best off wrapping the call inside a function of your own as you did in your first example.
The history
attribute of window
simply references the History interface, and can only be invoked globally.
The failing method uses an element context, which has no history
attribute.
本文标签: javascriptwindowhistoryback illegal invocationStack Overflow
版权声明:本文标题:javascript - window.history.back illegal invocation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743931845a2563988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论