- editing / quetz: If you have problems running JavaScript on the page, be sure to check out my findings on the topic that I posted at http://social.msdn.microsoft.com/Forums/en-AU/jscript/thread/bac9f056 -e0de-449e-a1b6-36e745fa59c6 - your problems may be related to some unsuccessful timings or property ordering. it turns out that if you hardcode the URL in XAML, then you need to make sure that in this XAML the value of IsScriptEnabled is set to the SOURCE property, otherwise you will have many crashes depending on what type of document you have. the same goes for setting up Source or Navigate'ing in the code, but this is pretty obvious. on the other hand, at first glance, bindings seem to work properly anyway. - / change
As long as your publication has passed, but if you still have a question in your memory, maybe this will help you a little.
Let first think shallow:
Perhaps the page is not loaded yet? You say that all pages are in ISO, so they load in no time, but it still takes some time. You have specified only the Navigate () method which is a little strange for me, because when InvokeScript () 'ing, the most important thing is to make calls that it makes or after receiving an event . Although you could implement it correctly, and you could be mistaken in the Navigated event using Navigate (), but according to what you wrote, you could also try:
void myfuntion() { webbrowser.Navigate("pages/index.html"); webbrowser.InvokeScript("myjsfunc"); }
which will almost never work. the correct way to implement it:
... //elsewhere webbrowser.Navigated += wb_Navigated; ... void myfuntion() { webbrowser.Navigate("pages/index.html"); // or webbrowser.Source = ... } void wb_Navigated(object sender, NavigationEventArgs e) { webbrowser.InvokeScript("myjsfunc"); }
only in this way can you be sure that the page is loaded. Without this, you are almost guaranteed to fail.
However, in this case you are likely to get 0x80020101 hresult.
This one, at least in versions 7.0, 7.1 and 7.5 sdk, is used when the JS engine encounters a character, it is not understood. This error has also been reported for many other cases, but this is the most common problem. Whenever you try to call enything containing undefined names, you will see 0x80020101. For example, InvokeScript ("eval", "askdjaslkdsajdla") will throw this, assuming that this variable is not created to fool the example :)
I am writing about this because if you DO NOT respond to Navigated and make calls prematurely, then the memory page may still have an old page with old content, and therefore the names that you are trying to tu link may be missing.
But, if you really see 80020006 (which is namenotfound, but from the OLE layer), I would suggest that the problem is in a different place. Then we have to dig deeper.
Whenever you try to call InvokeScript during the Navigated-event-handler, you may be the victim of a very random (at least from my experience) error in a web browser. Simple: sometimes, despite receiving a Navigator notification, WebBrowser is not yet in a fully prepared / visualized / wtf state.
I do not know exactly when, I believe, it is related to page size, script size / number or the number of external related resources.
When this happens, you will see a ComException other than 0x80020101, and I'm pretty sure it will be 80020006. Simple InvokeScripts like ("eval", "mom") can execute and execute correctly, but more complex ones, lower chances . For example, when this happens, all scripts that link directly to the body element, or its child elements, immediately crash when this symbol resolves. If you use libraries such as jQuery, you can not indicate that the reference to the body is explicitly indicated - if the library affects it, it will work in exactly the same way, with the same exception. Oddly enough, document.head is not affected by the problem, and you can refer to it without problems. From my observations in such cases, only the body is problematic.
Actually, after writing this - I see that, probably, your case may be the same as you try to change "document.body.style.fontSize" ..
In any case, there is no instant way to solve the problem, because the problem is somewhere in the internal timings of the web browser, and it seems that โNavigationโ is being reported too soon. I'm sure this is a terribly bug in the web browser, and maybe they will be fixed in some future version. As long as this is the only way to handle this, it is ... deferring your call until the browser really finishes parsing the page.
To put it another way: If you try to call InvokeScript correctly in the browser.Navigated handler, you may get an exception other than 0x80020101 (which would mean that your js code specified for InvokeScript is badly formed). If you catch such an error (possibly 80020006), check to see if you can touch document.body through invokescript. if you get another error, then it is not. But, if you get the EXACT error, then most likely it is, and the problem is that you are out of luck with your or web browser. You must abort your attempts, start the timer 125..500 milliseconds, return from the handler so that the browser is allowed to continue its internal tasks, and then when the timer expires, repeat the initial call and pray. And during this delay, you should also listen for exceptions in the same way, because you can get the same error again, and again and again (..).
Of my experiments, in such cases, 500 ms was always enough, but it was ugly for the user. 125 was completely invisible, but then - sometimes it was too fast, and 2-3 attempts were made.
But, no matter what happens - as soon as you navigate, get Navigated, and then successfully tap document.body - timeerror will no longer be repeated for this page. As soon as the internal mechanisms of the browser are launched, InvokeScript will work like a charm, well, until you load the next page ..;)
From my experience, if you can control the source of the page, you can also try to simplify the workaround a bit - on your page, somewhere in the body, put:
<script type="text/javascript"> window.external.notify('somestringyouwillknow'); </script>
and in your code, C # or XAML attaches to the WebBrowser.ScriptNofity event. Whenever your page loads, it calls this ScriptNotify and provides you with the line you wrote there. It will definitely arrive after the Navigated event, and if you understand this ScriptNotify notification, it is obvious that the JS engine is loaded and that the body is at least partially analyzed, and may also be initialized and touchable. document.body / p>