# How to close popup when screen changes?

**URL:** <https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690>\
**Category:** Ignition\
**Tags:** ignition81, ignition80, perspective\
**Created:** [January 17, 2023, 8:39pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690 "2023-01-17T20:39:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANKIT\_BARAD](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@ANKIT\_BARAD](https://forum.inductiveautomation.com/u/ANKIT_BARAD)\
**Post date:** [January 17, 2023, 8:39pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/1 "2023-01-17T20:39:53Z")

</div>

Does anyone know how to close popup automatically when I change the view on perspective?  
I do have many popups on each screen and I do not want to close them pressing button all the time.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 17, 2023, 9:22pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/2 "2023-01-17T21:22:47Z")

</div>

You should be able to use the `onShutdown` event on a view, to send a message. Any popup that is open and listening for that message would then be able to close using `system.perspective.closePopup()`

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [January 17, 2023, 9:28pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/3 "2023-01-17T21:28:19Z")

</div>

Since the main responsibility of closing a Popup rests with the Popup itself there isn't truly a dedicated route for closing all Popups which are open.

If your users chnage pages by maniupating the URL bar directly, there's nothing you can do. If you're managing the navigation through Actions, then you should include a script along these lines anywhere which would perform navigation:

```auto
count_of_potenital_popups = 5
for i in range(count_of_potenital_popups):
    # specifying a blank string for the Popup id acts against the most recently focused Popup
    system.perspective.closePopup(id="", pageId=self.page.props.pageId)

```

---

<div class="post-metadata">

**Author:** ![ANKIT\_BARAD](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@ANKIT\_BARAD](https://forum.inductiveautomation.com/u/ANKIT_BARAD)\
**Post date:** [January 18, 2023, 1:17pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/4 "2023-01-18T13:17:38Z")

</div>

Do I need to write all the popup in the script?

---

<div class="post-metadata">

**Author:** ![ANKIT\_BARAD](https://avatars.discourse-cdn.com/v4/letter/a/6f9a4e/32.png) [@ANKIT\_BARAD](https://forum.inductiveautomation.com/u/ANKIT_BARAD)\
**Post date:** [January 18, 2023, 1:18pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/5 "2023-01-18T13:18:56Z")

</div>

> [@cmallonee](#):
>
> aniupating the URL bar directly, there's nothing you can do. If you're managing the navigation through Actions, then you should include a script along these lines anywhere which would perform navigation:

Do I need to write IDs for all the popup on a screen?

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [January 18, 2023, 1:59pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/6 "2023-01-18T13:59:14Z")

</div>

Perhaps the docs will be helpful for you,  
[https://docs.inductiveautomation.com/display/DOC81/system.perspective.closePopup](https://docs.inductiveautomation.com/display/DOC81/system.perspective.closePopup)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 18, 2023, 2:08pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/7 "2023-01-18T14:08:08Z")

</div>

If you want to close all popups that are open, at some point you will have to indicate to the system to close that popup. There is no "close all popups" short cut.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [January 18, 2023, 6:02pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/8 "2023-01-18T18:02:45Z")

</div>

In addition to the quick-and-dirty avenue I already supplied, there's two other ways to do this.

**More correct:**  
In the `onShutdown` Event script for the View in question, close out all "known" Popups for the View.

```auto
known_popup_ids = ["a", "b", "c"]
for popup_id in known_popup_ids:
    system.perspective.closePopup(id=popup_id)

```

This requires you to always update the list of potential Popups, so this is inherently brittle as you develop and add more content. Additionally, there is always the possibility of other Views (Popups, Docked Views) opening their own Popups, and this View would know nothing about them.

**Most correct:**  
In the `onShutdown` Event script for the View in question, broadcast that all Popups should be closed for the page.

```auto
system.perspective.sendMessage(messageType="CLOSEPOPUPS", scope="page")

```

This route requires that EVERY POPUP have at least one component which has a configured Message Handler listening at the page scope for a "CLOSEPOPUPS" message. It also requires that the Popup either knows its own ID (passed in as a parameter perhaps, or hard-coded as a custom prop). You are not able to rely on supplying an empty-string id because there could be other Popups which would be closed.

```auto
system.perspective.closePopup(id="MyKnownId")

```

This route is the "most correct" because there is no list of Popups to manage as development continues, and it allows the individual Popups to manage their own lifecycle. Also, this route will also act on Popups opened from other Views as long as they too have a configured Message Handler and ID.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 18, 2023, 6:25pm UTC](https://forum.inductiveautomation.com/t/how-to-close-popup-when-screen-changes/68690/9 "2023-01-18T18:25:17Z")

</div>

> ... when screen changes?

To make things clear, you should edit your title to be when _view_ changes. There are no "screens" in Perspective. The correct is always helpful.
