Discussion:
Detect jscript close in WebBrowser control
(too old to reply)
Luc
2009-12-30 09:17:01 UTC
Permalink
I've got a combination of a windows forms app hosting a webbrowser control,
and a web application that's displayed in that webbrowser control.

At a certain point, the web application registers a startup script that
calls window.close(). The webbrowser control is supposed to terminate the
application at that moment.

When this is executed, I've got two problems (the first isn't that serious,
the second is the show stopper):

* The webbrowser control pops up the dialog asking if I want to close the
browser, just like IE does. I'm fine with IE doing that, but can it be
avoided for a webbrowser control?

* I find no way to detect this in the forms application. Apart from
popping up that dialog, the webbrowser control doesn't seem to do anything
with it, no matter what action the user chooses.
unknown
2010-05-27 06:36:53 UTC
Permalink
This snippet will solve your problems both.

// window.external.Test('will be called from script code')
public void Test(string s)
{
MessageBox.Show(s);
This.Close();
}


private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
webBrowser1.Url = new Uri(@"http://someUtlthathasclosebutton.com");
// set this variable true if you're debugging
//webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// overrides javascript window.close() so you won't have the annoying pop-up window
// calls TestMethod in our form and passes the parameteres from DOM to our managed code

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function window.close() { window.external.Test('hey from script') } ";
head.AppendChild(scriptEl);
}




Luc wrote:

Detect jscript close in WebBrowser control
30-Dec-09

I have got a combination of a windows forms app hosting a webbrowser control
and a web application that is displayed in that webbrowser control

At a certain point, the web application registers a startup script tha
calls window.close(). The webbrowser control is supposed to terminate th
application at that moment

When this is executed, I have got two problems (the first is not that serious
the second is the show stopper)

* The webbrowser control pops up the dialog asking if I want to close th
browser, just like IE does. I am fine with IE doing that, but can it b
avoided for a webbrowser control

* I find no way to detect this in the forms application. Apart fro
popping up that dialog, the webbrowser control does not seem to do anythin
with it, no matter what action the user chooses.

Previous Posts In This Thread:

On Wednesday, December 30, 2009 4:17 AM
Luc wrote:

Detect jscript close in WebBrowser control
I have got a combination of a windows forms app hosting a webbrowser control
and a web application that is displayed in that webbrowser control

At a certain point, the web application registers a startup script tha
calls window.close(). The webbrowser control is supposed to terminate th
application at that moment

When this is executed, I have got two problems (the first is not that serious
the second is the show stopper)

* The webbrowser control pops up the dialog asking if I want to close th
browser, just like IE does. I am fine with IE doing that, but can it b
avoided for a webbrowser control

* I find no way to detect this in the forms application. Apart fro
popping up that dialog, the webbrowser control does not seem to do anythin
with it, no matter what action the user chooses.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Task Parallelism in C# 4.0 with System.Threading.Tasks
http://www.eggheadcafe.com/tutorials/aspnet/21013a52-fe11-4af8-bf8b-50cfd1a51577/task-parallelism-in-c-4.aspx
Loading...