Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: Failed to execute 'getComputedStyle' on 'Window': parameter is not of type 'Element'

In short: I am trying to understand the meaning of this TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element' The error appears while lunching Mediawiki's VisualEditor, as can be seen here:

http://www.wiki.org.il/index.php?title=new-page&veaction=edit

The error doesn't enables creating new pages or editing the wiki anonymously. However, with the use of a different skin the error disappears:

http://www.wiki.org.il/index.php/Main_Page?useskin=vector

The wiki runs on 1.25alpha.

like image 239
OpenFox Avatar asked Mar 22 '15 16:03

OpenFox


2 Answers

I had this same error showing. When I replaced jQuery selector with normal JavaScript, the error was fixed.

var this_id = $(this).attr('id'); 

Replace:

getComputedStyle( $('#'+this_id)[0], "") 

With:

getComputedStyle( document.getElementById(this_id), "") 
like image 186
Anish Avatar answered Sep 17 '22 13:09

Anish


The error message says that getComputedStyle requires the parameter to be Element type. You receive it because the parameter has an incorrect type.

The most common case is that you try to pass an element that doesn't exist as an argument:

my_element = document.querySelector(#non_existing_id); 

Now that element is null, this will result in mentioned error:

my_style = window.getComputedStyle(my_element); 

If it's not possible to always get element correctly, you can, for example, use the following to end function if querySelector didn't find any match:

if (my_element === null) return; 
like image 24
Ryszard Jędraszyk Avatar answered Sep 16 '22 13:09

Ryszard Jędraszyk