Error trying to execute JavaScript after navigating pages in WebBrowser

I use the WebBrowser control in a WP7 application and set IsScriptEnabled to true. Then, when I try to call the script through InvokeScript WebBrowser, I see a strange behavior: the script runs correctly on the first page. Then I go to another page through Navigate() and try to execute the script. I get the following error message:

"An unknown error has occurred. Error: 80020006."

I tried setting IsScriptEnabled to true before moving on to the next page - no luck. Waiting for the completion of the document loading state also does not help.

All HTML files are stored in isolated storage and displayed correctly. Only script interaction does not work.

Any ideas?

+4
source share
4 answers

- 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>

+5
source

80020006 may be displayed for various reasons due to the inability to perform the required function. This usually happens when a function cannot be found.
Make sure that you download the correct version of the file that contains this function, and not the old cached version.
Phone caching can be very aggressive, so please do not assume that you have the latest version of the downloaded file installed. If in doubt, change the contents of the files so that you can see that this is the latest / corresponding version.

Update
It looks like you are trying to execute code with arbitrary pages viewed on the Internet, you may have the same problem as documented at http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/e00942e4-e40c -4e80-b112-30ca0709fbc8 /

It could also be related: http://social.msdn.microsoft.com/Forums/en-US/mktplace/thread/4ae5f139-f8b2-495b-860f-01075b735ee7/

+1
source

Make sure all of your pages have a DOCTYPE tag, something like

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

I also found that including a javascript piece, even an empty pair of tags also helps:

 <html> <head> <script></script> ... <head> <body> ... </body> </html> 
0
source

I saw this error in three situations:

  • When JavaScript fails in the function on the page you are calling. Try debugging the page in Chrome, with a particular focus on the inspector. You may need to add some scaffolding to play as your C # code during this debugging process.
  • When the WebBrowser not bound correctly to the XAML layout. For example, when the control is in the grid, but does not have the Grid.Row or Grid.Column , or if they are outside the borders.
  • When using the WebBrowser.Loaded event instead of the WebBrowser.Loaded event as a trigger to call InvokeScript()
0
source

All Articles