问题
This website : http://blog.joins.com/media/folderListSlide.asp?uid=ddatk&folder=3&list_id=9960150
has this code:
<script>alert('¿Ã¹Ù¸¥ Çü½ÄÀÌ ¾Æ´Õ´Ï´Ù.');</script>
So my web browser control show a popup, how can I bypass the popup without using sendkeys enter??
回答1:
In the ProgressChanged event handler, you insert a script element that replaces the Javascript alert function with a function of your own, that does nothing:
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = "window.alert = function () { }";
element.text = alertBlocker;
head.AppendChild(scriptEl);
}
}
For this to work, you need to add a reference to Microsoft.mshtml and use mshtml; in your form.
回答2:
If you intend not to ever use the alert() function on your page, you can also just override it. E.g.:
<script type="text/javascript">
alert = function(){}
</script>
If you do need to use JavaScript's alert function, you can 'overload' it:
<script type="text/javascript">
var fnAlert = alert;
alert = function(message,doshow) {
if (doshow === true) {
fnAlert(message);
}
}
alert("You won't see this");
alert("You will see this",true);
</script>
回答3:
handle IDocHostShowUI::ShowMessage and return S_OK. Check http://www.codeproject.com/KB/miscctrl/csEXWB.aspx for an example.
回答4:
solution given is wrong
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = "window.alert = function () { }";
element.text = alertBlocker;
head.AppendChild(scriptEl);
}
}
Seems handling a windows hook for message is solution
回答5:
I think you are navigating a page within alert(xxx) in its javascript using WebBroswer in a WinForm application? You can try:
broswer.Navigated += (sender, args) =>
{
var document = (sender as WebBrowser).DocumentText;
//find the alert scripts and remove/replace them
}
回答6:
You can disable all popups by setting
webBrowser.ScriptErrorsSuppressed = true;
Despite the name, this settings actually blocks all popups, including alert()
来源:https://stackoverflow.com/questions/3848632/stop-alert-javascript-popup-in-webbrowser-c-sharp-control