最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript eval() fails with window.external.notify(), works with window.alert() - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Jun 2, 2023 at 20:15 Wahlstrommm 6462 gold badges8 silver badges21 bronze badges asked Sep 17, 2013 at 22:56 BobHyBobHy 1,73214 silver badges28 bronze badges 2
  • This may be related to whatever is in your HTML. It might be trigger quirks mode or similar. Can you show a full repro of the issue? – Matt Lacey Commented Sep 18, 2013 at 8:39
  • 1 Thank you for providing the wokraround in (3). God bless you. – Victor Ionescu Commented Jun 16, 2014 at 13:33
Add a ment  | 

2 Answers 2

Reset to default 1

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.

For executing JavaScript code in WebView from C# use InvokeScript and getting values from java script functions in C# use window.external.notify in java script function. to catch value in C# from java script functions use following code.

private void Web_OnScriptNotify(object sender, NotifyEventArgs e)
{
    Debug.WriteLine("Called from ScriptNotify! {0}", new[] { e.Value });
}
发布评论

评论列表(0)

  1. 暂无评论