On WP8, if I execute (1):
Microsoft.Phone.WebBrowser wb;
wb.InvokeScript("eval", "window.external.notify('abc');");
It throws a 'target of invocation returned an error', unknown error, hresult 80020101. But (2)
wb.InvokeScript("eval", "window.alert('abc');");
works fine, and displays the message box.
And (3)
wb.InvokeScript("eval", "( function (){window.external.notify('abc');})();");
Also works fine.
My question is, what is it about window.external.notify() that prevents eval from invoking it directly? It is a function call, like window.alert(), so it should be a valid script. But if there is something special about the unadorned call in 1), then why does the wrapped call in 3) work?
I understand that eval() is the root of all evil, and I have read other SO posts relating to eval() problems with a function definition. (Where would we all be without SO?) But this is clearly a different problem.
I think it is related with context of calling that eval(...)
.
If you call eval("window.external.notify('abc');")
, the script should be called on the global window
context.
You can check the context as below to pring the current context:
eval("console.log(this); window.external.notify('abc');")
Then try to test in those 3 ways to check is there any difference about the context.
To specify one context to run, you can use call
or apply
to set context with first param.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With