0

I am looking for a way to get the string of JavaScript alerts messages instead of showing it as message box,now, I am using a code to block alerts form a web page, this is the code (c#):

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

I have this in web browser control navigated event.

I am very new to JavaScript that's why :)

Thank you very much.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
AymAn AbuOmar
  • 403
  • 1
  • 5
  • 16

1 Answers1

1

string alertBlocker = "window.alert = function (msg) {document.write(msg);}";

there you can write the message to a hidden div/span/etc and then get that hidden div's innerText.

eventHandler
  • 1,088
  • 12
  • 20
  • thank you very much, but as i said above i am so new to HTML, could you please show me how to do that ? :) or to give me a guide to start with ? Thanks – AymAn AbuOmar Mar 28 '14 at 23:31
  • here's a link that might help you in creating a new div element. http://stackoverflow.com/questions/19494339/creating-dynamic-div-using-javascript and this to make that div invisible and not shown http://stackoverflow.com/questions/9456289/how-to-make-a-div-visible-and-invisible-with-javascript those are using pure javascript. there is also a javascript library called jQuerry that is very usefull for making things easier, but i'm not very familiar with it.. – eventHandler Mar 30 '14 at 22:27