Search code examples
javascriptvbscriptjscript

How to Exit Programatically Created Browser in JScript


I have the following JScript which opens Internet Explorer, navigates to the current users' Lync Response Group Settings page, and clicks on one of the checkboxes found there:

var ie = WSH.CreateObject('InternetExplorer.Application');
    url = "https://LyncServer.DOMAIN.co.uk/RgsClients/Tab.aspx",

ie.visible = true;
ie.Navigate(url);
while (ie.readyState != 4) WSH.Sleep(25);

ie.document.getElementById('ctl05_ctl00_ctl04_ctl00_ctl00_ctl01').click();

Once this is done, I want the script to close the browser window.

I tried the following:

ie.exit()
ie.close()
ie.document.exit()
ie.document.close()    

But got this error:

Object doesn't support this property or method


Solution

  • TLDR: Use ie.Quit().


    This has nothing to do with VBScript vs. JScript. What you call "the JavaScript close command" is the DOM (not JS) method (not "command") of the Window object. Usually the DOM is the thing you manipulate with JS code that runs inside the browser as part of the HTML it's displaying.

    This is unrelated to the InternetExplorer automation object. This method describes the Internet Explorer program, not the HTML page that is displayed in it right now. For example, it has a property that controls whether or not you're in "Offline browsing" mode. That's not part of the DOM.

    This object has a bunch of other methods and properties which are mostly documented in the MSDN page I linked above - InternetExplorer object. Specifically, it implements the IWebBrowser2 interface which means it implements the IWebBrowser2::Quit method.

    Which is, obviously, unrelated to the WSH WScript.Quit(errorCode) method. Their signatures aren't even compatible. The WSH method returns an (optional) error code, the IE method doesn't.