(IE9)Unable to get property 'dispatchEvent' of undefined or null reference - javascript

My code is:
var internal= $("span[title='expand']")[0];
var clickEvent = document.createEvent("MouseEvent");
clickEvent.initEvent("click",true,true);
internal.dispatchEvent(clickEvent);
and IE9 throws runtime error
Unable to get property 'dispatchEvent' of undefined or null reference
I am confused because IE9 starts supporting dispatchEvent. But IE10 AND IE11 works fine with the same code.
How can I solve it? Thanks!

Are you sure the local variable 'internal' is not undefined at the moment dispatchEvent is called?
Did the call to jQuery to fetch the span return a valid DOM Node?

Related

offset() from jQuery crashes on "elem.getClientRects is not a function"

const targetPos = target.offset();
I am getting this error:
Uncaught TypeError: elem.getClientRects is not a function
The target is not a window. jQuery version is 3.2.1 and I am not using jQuery UI.
Any ideas why is it crashing and how to fix it?
Oh, it turns out target was a wrong object (#adeneo was probably suspecting this).
When passing it to a function which expects the target I forgot that ElementRef (element field) is not actually the DOM element I wanted, it's just a wrapper.
I changed
jQuery(angularComponent.element)
to
jQuery(angularComponent.element.nativeElement)
and now it works :).

XPathResult is undefined

I have the error "Uncaught TypeError: Cannot read property 'FIRST_ORDERED_NODE_TYPE' of undefined" in some cases (not always) when run next code:
var type = XPathResult.FIRST_ORDERED_NODE_TYPE;
At the same time next code works fine for the same cases:
if (XPathResult)
var type = XPathResult.FIRST_ORDERED_NODE_TYPE;
It seems error is raised by interpretator, but not in runtime. Probably the problem is related with activation of window object.
I have this workaround with check if object exists but i want to figure out what is the real problem. Any ideas?
UPD: The problem is reproducableonly in Chrome

Uncaught TypeError: Illegal invocation in Javascript (geolocation)

I am getting the Javascript error "Uncaught TypeError: Illegal invocation" when running the code
var nativeGeoloation = window.navigator.geolocation.getCurrentPosition;
nativeGeoloation(function (){ alert("ok")});
I have tried calling the code in the context of the window, but get the same error:
nativeGeoloation.call(window,function (){ alert("ok")})
The background of this question is that I am trying to access the native version of the geolocation function that has been overwritten by another javascript library (cordova)
The error I'm getting in Firefox is:
TypeError: 'getCurrentPosition' called on an object that does not implement interface Geolocation.
Change your code to:
var nativeGeoloation = window.navigator.geolocation;
nativeGeoloation.getCurrentPosition(function (){ alert("ok")});
(Note, you've also spelled nativeGeoloation incorrectly which might cause you problems down the road if you start to spell it correctly).
DEMO

JavaScript error: SCRIPT5007: Unable to get property 'getElementsByTagName'

I am using the following jQuery to get the field value from a Display Form in SharePoint 2013 so I can pass the value along in a URL.
var itemID = $('h3:contains("My ID")').closest('td').next('td').text();
It is working perfectly, but I am getting an error when I inspect the action:
SCRIPT5007: Unable to get property 'getElementsByTagName' of undefined or null reference
File: sp.ui.dialog.js, Line: 2, Column: 22380
This contradicts what is actually occurring, since the code executes. I am running IE11 in IE10 compatibility mode.
I have seen this when the URL being passed to the modal function was invalid. In my case, I has a dynamic URL containing a variable named itemID. I got the error whenever itemID was undefined.
I wish I could elaborate further but all I can comment on is my own observations at this point.

How to fix Window.getComputedStyle is not an object error

I am trying to use draggabilly available at
https://github.com/desandro/draggabilly
it works fine in html. Now when i have tried to add it in wordpress. I am getting following error in firebug console.
TypeError: Argument 1 of Window.getComputedStyle is not an object.
return s.getComputedStyle(t, null)
here is a link for js file
http://draggabilly.desandro.com/draggabilly.pkgd.min.js
You are calling init twice. Go through your code and remove one instance.
I received this error testing my project with IE8: finally it was so obvious, this method doen't work with IE 8!
Error: Object doesn't support this property or method at: http://...
I received this error with FF that does support this method but I forgot to change window to my frame window object!
console.log(getComputedStyle(window.document.querySelector('.tip > .tip.top'), ':after').getPropertyValue('left'));
TypeError: Argument 1 of Window.getComputedStyle is not an object.
at: http://...
Note that the above error pops up even if your window object is ok but the querySelector returns nothing! (I suspect it's your case).

Categories