Open web popup from WPF application and close it with javascript -
i have wpf application communicates 3rd party server.
after user completed task in wpf tool, need display specific web site server in popup window. popup must closed using 1 of buttons on site (which confirm task). when javascript wants close window, internet explorer open dialog box , ask user confirm closing popup.
i looking simple solution disable dialog box. can't change behaviour of server need work around it.
my old approach annoying confirmation dialog run iexplore.exe popups url parameter (via system.diagnostics.process
).
my new approach hidden <frame x:name="popupframe" />
element. when popup should openend set source
attribute of popupframe page opens popup. popup has base window , javascript can close without confirmation dialog. doesn't work on several systems (probably because of security settings) , involves page redirect/popup.
is there easier way that?
update: ok, realized don't have control on web page, can think of injecting javascript page, add event handler webbrowser's loadcompleted event , inject javascript there, should take care of , not require modify original web page:
// add reference microsoft.mshtml using mshtml; // inside window constructor after set // webbrowser's objectforscripting property this.webbrowser.loadcompleted += this.webbrowser_loadcompleted; void webbrowser_loadcompleted(object sender, navigationeventargs e) { var doc = (htmldocument)webbrowser.document; var head = doc.getelementsbytagname("head").cast<htmlheadelement>().first(); var script = (ihtmlscriptelement)doc.createelement("script"); script.text = "javascript:window.onbeforeunload = function () { window.external.close(); }"; head.appendchild((ihtmldomnode)script); }
it sounds own webpage opening here can do:
1.- add popup control window , add webbrowser control it, you'll use navigate page:
<button content="button" click="button1_click" /> <popup x:name="popup" width="300" height="300"> <webbrowser x:name="webbrowser"> </webbrowser> </popup>
2.- create new class , add [comvisible(true)] attribute it. should include reference of popup control object. add public method new class hides popup control.
using system.runtime.interopservices; using system.windows; using system.windows.controls.primitives; [comvisible(true)] public class scripthandler { private popup popup; public scripthandler(popup popup) { this.popup = popup; } public void close() { this.popup.isopen = false; } }
3.- set webbrowser control's objectforscripting property instance of new class.
public partial class mainwindow : window { private scripthandler scripthandler; public mainwindow() { initializecomponent(); this.scripthandler = new scripthandler(this.popup); this.webbrowser.objectforscripting = this.scripthandler; } // shows pop , navigates page private void button1_click(object sender, routedeventargs e) { this.webbrowser.navigate("http://localhost:56977/popup.htm"); this.popup.isopen = true; } }
4.- since registered scripthandler object webbrowser control public methods in class can accessed javascript, add javascript method page (you may need verify 'undefined' validation i'm doing) calls close method in comvisible class:
<script type="text/javascript"> function closeme() { if(window.external != null && window.external.closeme != 'undefined') { window.external.close(); } } </script>
5.- add link calls javascript closeme method page:
<a href='javascript:closeme()'>close me</a>
as realized solution work not popup control, can instantiate new window , close it, dispose of webrowser control etc. or show/hide different type of control.
Comments
Post a Comment