vb.net - Web browser control, modal window/popup to STAY INSIDE web browser control for Visual Studio 2015/Visual Basic 2015 -
this first time i'm posting question here; have searched , searched , searched here , other places , cannot seem results. i'm using visual basic 2015 in visual studio 2015. question: need have modal window/popup particular website remain inside web browser control/window on form (webbrowser1); when particular link clicked, modal window/popup jumps out of form , directly user on screen. have keep popup inside because there other links clicked on popup, if jumps out of web browser control, no code work since it's outside webbrowser1. have found code older versions, , not 2015; if can add webbrowser2 have popups/modal windows appear there if possible, long can code them keep clicking inside form. please help! thank you!
window.open
(and click on <a target="_blank">
etc) can handled via newwindow2 event. hans pointed out how in comments. newwindow3 works too, need @ least windows xp sp2.
as window.showmodaldialog
, bit tricky. ie has idispatchex (wrapped iexpando in .net) implemented on scripting objects replace methods , properties own implementation. window.showmodaldialog shows dialog has arguments , return values, need override properties in modal dialog create too. code looks tis:
void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { //skip events frames if(webbrowserreadystate.complete!=webbrowser1.readystate) return; if(findloginformonpage()) {dologin();return;} if(iswelcomepage()){navigatetopage1();return;} if(ispage1()){submitformonpage1();return;} if(ispage1formresult()){ var document=webbrowser1.document.domdocument mshtml.itmldocument2; var expando =(iexpando)document.parentwindow; expando.removemember(expando.getmethod("showmodaldialog" ,bindingflags.instance | bindingflags.public); expando.addmethod("showmodaldialog" ,new showmodaldialogdelegate(this.myshowmodaldialog)); } ...... } object myshowmodaldialog(string url, object varargin, object options) { using(frommyshowmodaldialog myshowmodaldialog =new myshowmodaldialog()) { myshowmodaldialog.startupurl=url; myshowmodaldialog.dialogarguments=varargin; //omit code parse options //and set dialog height/width/topleft location etc if(myshowmodaldialog.showdialog()==dialogresult.ok) { //do on return value before passing scripts ...... return myshowmodaldialog.returnvalue; } return null; } }
and in load event handler of myshowmodaldialog call webbrowser1.navigate show page requested parent page.
now need pass arguments webbrowser control on new form. same above replace property time.
expando.removeproperty("dialogarguments"); expando.addproperty("dialogarguments") .setvalue(expando,this.dialogarguments);
this let web page access value passed myshowmodaldialog , stored in this.dialogarguments.
the earliest can access dom in webbrowser1_documentcompleted. time scipts on page read window.dialogarguments executed , got nothing. after overriding window.dialogarguments, need study script on page find out how revert that. example, if page has
<head> <script> var omyobject = window.dialogarguments; var sfirstname = omyobject.firstname; var slastname = omyobject.lastname; </script> ... <span style="color: 00ff7f"> <script> document.write(sfirstname); </script> </span>
you need change values of sfirstname , slastname change innertext property of span, identify via relationship named div or table cell. can write necessary changes in script , call via htmldocument.invokescript.
if page returns value parent, need pass on parent form too. override window.returnvalue when script writes window.returnvalue writes variable provided
...... expando.removeproperty("returnvalue"); expando.addproperty("returnvalue").setvalue(expando,this.returnvalue);