Change a C# Variable with InvokeScript

前端 未结 2 1588
我寻月下人不归
我寻月下人不归 2021-01-03 16:57

I need to check if a WebBrowser control in my Windows Phone app has a history, and the way that I figured out how to do that is by using browser.InvokeScript(\"eval\",

2条回答
  •  梦谈多话
    2021-01-03 17:37

    hasHistory = (bool)browser.InvokeScript("eval", "return (history.length > 0);");
    

    The method InvokeScript returns an object that is the object returned by the script you executed.

    The following code is a bit hackish, but seems to work fine for most cases.

            bool hasHistory = false;
            try
            {
                webBrowser1.InvokeScript("eval");
                hasHistory = true;
            }
            catch (SystemException ex)
            {
                hasHistory = false;
            }
    

提交回复
热议问题