Er. Bahuguna Goyal
Er. Bahuguna Goyal

Reputation: 192

I want to close the firefox browser which is opened through backend C# process

I want to close the firefox browser which is opened through backend C# process. All other browsers support window.close(); opened through C# process. But firefox does not support. My code in C# Process.Start("firefox", "http://www.google.com"); After opening the browser i am calling this js code. window.close(); all other browsers working perfectly. but FF. if you can help me on this.

Upvotes: 0

Views: 619

Answers (1)

John Wu
John Wu

Reputation: 52280

According to this documentation, script should not be able to close a window that it did not originally open:

The Window.close() method closes the current window, or the window on which it was called.

This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

So, while it works in IE and Chrome today, don't count on it working forever.

If you want to close the window programmatically, it seems like your best option may be to close the process from your c# code, e.g.

var p = Process.Start("firefox", "http://www.google.com");
System.Threading.Thread.Sleep(10000); //Wait 10 seconds
p.CloseMainWindow();

Upvotes: 1

Related Questions