Welcome Guest Search | Active Topics | Sign In | Register

WebBrowser Print Error With New Window Options
AndrewS
Posted: Friday, September 13, 2019 2:36:24 PM
Rank: Member
Groups: Member

Joined: 4/28/2016
Posts: 13
I'm trying to use EO.WebBrowser with a website that opens a new tab and prints a pdf, but encountering an error.

When printing the website does

Code: JavaScript
var w = window.open();
w.document.write("HTML here");


and EO gets the error "Webkit console message. Severity Error. Source https://www.mywebsite.com. Message Uncaught TypeError: Cannot read property 'document' of null. Line Number 1698"

I'm handling the NewWindow event as follows

Code: C#
void WebView_NewWindow(object sender, NewWindowEventArgs e)
        {
            // Cancel loading new window
            EO.WebBrowser.WebView webView = (EO.WebBrowser.WebView)sender;
            webView.Url = e.TargetUrl;
}


Is there a way to handle the NewWindow event where I navigate to the new URL within the current window (not opening a new tab) which also avoids this error?
eo_support
Posted: Friday, September 13, 2019 2:53:40 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,218
Hi,

You must display the new WebView and then set e.Accepted to true in order for the newly created WebView to live. Otherwise it will be destroyed and your w will be null. See here for more details on handling NewWindow event:

https://www.essentialobjects.com/doc/webbrowser/advanced/new_window.aspx

If you still have any question please feel free to ask.

Thanks!
AndrewS
Posted: Friday, September 13, 2019 4:21:33 PM
Rank: Member
Groups: Member

Joined: 4/28/2016
Posts: 13
Thank you for the quick reply. Is there a way to display the new WebView within the current window? In other words, I want to avoid opening a second tab since this is for a kiosk and multi-tabbed browser isn't desirable.
eo_support
Posted: Friday, September 13, 2019 5:02:51 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,218
Hi,

You could but then you would have to change your JavaScript code in your page. Your original JavaScript code requires both WebView objects and their corresponding JavaScript context (such as window and document object) to be alive. Specifically:

1. When you call var w = window.open(), this code runs inside the original WebView's JavaScript context. If you try to load another page into this WebView, then this JavaScript context will be destroyed. That means what happens with the code that are trying to run is undefined. It could be abruptly terminated, it could run into an exception, etc. The net result is whatever your code was trying to do won't finish properly;

2. When you subsequently access w.document, you are accessing the new WebView's JavaScript context. Your original error is the result of this JavaScript context not being valid (because you did not allow the new WebView to complete creation);

So the bottom line is whatever you do, your JavaScript code must match your NET code. You can't have JavaScript code expects two WebViews but only create one on the .NET side. In another word, if you choose to only maintain one WebView on the .NET side, you must change your JavaScript code not to rely on two.

Hope this makes sense to you.

Thanks!
AndrewS
Posted: Friday, September 13, 2019 5:05:25 PM
Rank: Member
Groups: Member

Joined: 4/28/2016
Posts: 13
That makes perfect sense, thank you!
eo_support
Posted: Friday, September 13, 2019 5:10:04 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,218
No problem. Please feel free to let us know if there is anything else.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.