Uncaught TypeError: Illegal invocation in Chrome
In your code you are assigning a native method to a property of custom object.
When you call support.animationFrame(function () {})
, it is executed in the context of current object (ie support). For the native requestAnimationFrame function to work properly, it must be executed in the context of window
.
So the correct usage here is support.animationFrame.call(window, function() {});
.
The same happens with alert too:
var myObj = {
myAlert : alert //copying native alert to an object
};
myObj.myAlert('this is an alert'); //is illegal
myObj.myAlert.call(window, 'this is an alert'); // executing in context of window
Another option is to use Function.prototype.bind() which is part of ES5 standard and available in all modern browsers.
var _raf = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame;
var support = {
animationFrame: _raf ? _raf.bind(window) : null
};
Uncaught TypeError: Illegal invocation html/JS
Your $docid
method does not work because it does not have a correct thisValue.
Binding document
to that method will fix that problem:
var $docid = document.getElementById.bind(document);
More on this
: https://stackoverflow.com/a/3127440/4949918
Uncaught TypeError: Illegal invocation in JavaScript
The console's log function expects this
to refer to the console (internally). Consider this code which replicates your problem:
var x = {};
x.func = function(){
if(this !== x){
throw new TypeError('Illegal invocation');
}
console.log('Hi!');
};
// Works!
x.func();
var y = x.func;
// Throws error
y();
Here is a (silly) example that will work, since it binds this
to console
in your make function:
var make = function(callback,params){
callback.call(console, params);
}
make(console.log,'it will be accepted!');
This will also work
var make = function(callback,params){
callback(params);
}
make(console.log.bind(console),'it will be accepted!');
JavaScript: TypeError: Illegal invocation but I'm not calling any function
This behavior is caused by the implementation of the specification. These DOM nodes are so-called 'platform objects' and the way they implement getters is slightly different from 'normal' javascript.
In short: they can not be extended without extra work.
var a = document.getElementById("something");
var b = Object.create(a);
When the baseURI
of b
is accessed, its this
points to b
which is not a valid 'platform object'. This causes the Illegal invocation
error. Accessing it through the prototype
does work because then its this
points to the prototype
which is a valid 'platform object'.
This comment on an issue of Chrome explains it in more detail and also supplies a workaround if you really need it: https://bugs.chromium.org/p/chromium/issues/detail?id=495437#c7
Keep getting Uncaught TypeError: Illegal invocation?
getElementById
requires a calling context of document
. Use .bind
to bind r
to document
:
var r = document.getElementById.bind(document);
TypeError: Illegal invocation in the pubads_impl_XXX.js in Mobile Safari
This seems to be a known ISSUE 1811 sentry-javascript and is related to blacklistUrls
not working on iOS9 Mobile Safari.
Suggested solution is to either update blacklistUrls
to use a regex or handle check in beforeSend
callback.
In the code attached in the issue, error was happening due to facebook crawlers, you can do similar check find out what url Google crawlers are using.
Sentry.init({
beforeSend: function (event) {
// Drop all events if query string includes `fbclid` string
if (location.search.indexOf('fbclid') !== -1) return null;
// Otherwise just let it though
return event;
}
});
Related Topics
Removing an Anonymous Event Listener
JSON.Stringify Doesn't Work with Normal JavaScript Array
How to Destructure Object Properties with Key Names That Are Invalid Variable Names
Onclick Function Runs Automatically
Vanilla JavaScript Event Delegation
String.Replace' Weird Behavior When Using Dollar Sign ($) as Replacement
Unexpected Behavior Using Array Map on an Array Initialized with Array Fill
How to Remove All the Options of a Select Box and Then Add One Option and Select It with Jquery
Prototype Based VS. Class Based Inheritance
Get Viewport/Window Height in Reactjs
What Is the Instanceof Operator in JavaScript
How to Dynamically Insert a <Script> Tag via Jquery After Page Load
Three.Js Generate Uv Coordinate
How to Call 3 Functions in Order to Execute Them One After the Other
Where to Write to Localstorage in a Redux App
Is Node.Js Native Promise.All Processing in Parallel or Sequentially