Search code examples
javascriptc#internet-explorermshtml

Running JS or JQ using execScript always throws syntax error in mshtml


The error code is "0x80020101" and does not specify an actual part of the code that has an issue.

The thing that really confuses me is that running the script through the console runs into no errors. The code looks like this:

int index = element_td.cellIndex; 
var scriptBody =
"function myFunc() {" +
"var row = document.querySelectorAll('#QueryModel_Provider_ProviderDropdownTable tbody tr')[" + index + "]; " +
"console.log(row);" +
"var clickEvent = document.createEvent('MouseEvents'); " +
"clickEvent.initEvent('mousedown', true, true); " +
"row.dispatchEvent(clickEvent);" +
"};" +
"myFunc();";
doc1.parentWindow.execScript(scriptBody);

It seems to think that there is an issue with the line: row.dispatchEvent(clickEvent); but I don't see anything wrong with it.

I know I can do this in a single line using JQ, but I was getting this error when doing that as well- hence trying the raw JS approach. (Though- worth noting, I didn't try using the code block in a function, it simply refused to acknowledge the $ in the first place)

I can fit all of it into one line- but code readability is more important right now.


Solution

  • Most IE versions do not support dispatchEvents, use the polyfill for custom events instead: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent