How to close the browser tab using UI Builder?

devashishku
Tera Contributor

Hi all,
I am struggling on a configuration where in a custom UIB portal, a new tab is opened in a browser and when i fill the form and submit, the form tab need to be closed. I do not want to navigate to any other route.

devashishku_0-1770630755878.png


Here, when i clicked on save button (UIB button), the current opened browser tab need to be closed.

I have tried using helpers.navigate.closeTab(); but not working.

Any suggestion, what I am doing wrong.

Thanks & Regards
Devashish Kumar

2 REPLIES 2

Nilesh Pol
Kilo Sage

@devashishku is this form is opened with window.open() function?

if yes, then you close it with window.close() function or

you redirect to home page with helpers.navigate.to("link_of_homepage"); instead of closing.

 

Terry_Yeti
Giga Expert

I ran your browser tab closing question through snowcoder ai. This is a common request but has some important browser security limitations to understand.

**The Short Answer:**

You cannot directly close a browser tab from UI Builder (or any web application) unless that tab was originally opened by your script. This is a browser security restriction, not a ServiceNow limitation.

**What You Can Do:**

If your page was opened via `window.open()` from another page, you can close it using a client script or declarative action with:

```javascript
window.close();
```

In UI Builder, you could wire this to a button click event using a "Run Script" declarative action.

**If the tab wasn't opened by script:**

The browser will block `window.close()` for security reasons. Your alternatives are:

1. **Redirect instead of close** - Navigate the user to a "thank you" or landing page:
```javascript
window.location.href = '/your-landing-page';
```

2. **Show a message** - Display instructions asking the user to manually close the tab

3. **For Service Portal widgets** - You can use `$window.close()` if the portal page was opened programmatically

**Practical UI Builder Approach:**

Add a button component, then in the Events panel, add a "Run Script" action on click:

```javascript
// Attempt to close - will only work if tab was script-opened
window.close();

// Fallback: redirect if close fails
if (!window.closed) {
window.location.href = '/now/nav/ui/home';
}
```

What's your specific use case? If you're trying to close after a form submission or workflow completion, there might be a better UX pattern to suggest.

_______________________________________
I used snowcoder ai to generate this. If you need to tweak the requirements, you can run it through their Yeti AI for free.