InfoPath 2010 close the browser window

How do you close the browser window (or tab) completely when you close an InfoPath browser form?

Quick steps:

  1. In your URL to open your form, specify the source parameter.  This is the URL InfoPath will return the user to after the browser form is closed.
  2. Create a simple SharePoint web page with a bit of JavaScript to close itself.
  3. Set the source parameter to this page.

 

1. Use the Source

MSDN documentation: http://msdn.microsoft.com/en-us/library/ms772417.aspx

When you close a browser form, either via the Close ribbon button, or via the Close action in rules, when the source parameter isn't specified, you end up on this page:

image

Figure: Silly and really redundant.

If you specify a source parameter, when the user closes the form the browser will redirect to that URL.  This could be the homepage of your site.

With Form Server you can use a few special values such as ~sitecollection or ~site

 

2. Create a web page that closes itself.

Create a page (wikipage or publishing page), and add this JavaScript magic here.

<script language="javascript" type="text/javascript">
    window.open("", "_self");
    window.close();
</script>

 

Why is this javascript so strange?  The reason is that if you just call window.close() directly, most browsers will check if the current window was opened from a parent window (such as a popup), and will throw an offensive prompt to the user.

image

Figure: JavaScript close window confirmation - we don't want this

 

There are a few different ways to trick the browser, the one that I use above basically tells the browser window to open itself, so now the browser thinks it's got a parent (which is itself), and now won't prompt the user for close confirmation.

 

3. Set the URL parameter

 

http://server/_layouts/FormServer.aspx?XmlLocation=/ProjectForms/227.xml&Source=~sitecollection/Pages/close.aspx&DefaultItemOpen=1

So now when you close your browser form, it will redirect to our close.aspx page, which will actually close the web browser window (or tab).