# Execute Script Every Time a Popup is Opened

**URL:** <https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [October 25, 2024, 4:20pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780 "2024-10-25T16:20:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan.perretta](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.perretta/32/75110_2.png) [@ryan.perretta](https://forum.inductiveautomation.com/u/ryan.perretta)\
**Post date:** [October 25, 2024, 4:20pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/1 "2024-10-25T16:20:48Z")

</div>

Hi all,

I am using a popup window that allows the user to enter a value into a Numeric Entry Field and submit. Because this is the only input field in the view I thought it would be useful to automatically set the focus to it when the popup opens.

However, it seems that the `onStartup` script only executes the first time the popup is opened in a session. After some reading on the startup event, it seems this is because the popup view is already loaded into the session after the first time.

My questions is: _Would it be possible to have this script execute every time the popup window is opened?_ Because the user would likely be using this multiple times in one session.

Component I am trying to set the focus on:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/4/54bb1b7c631f97eb85db596ced27c85129f3899c.png)

onStartup event script on the root container:

```py
self.getChild('AmountContainer').getChild('AmountInput').focus()

```

UPDATE: it seems to be working not _only_ on the first time the popup is opened, however it is not consistently working properly and I can't seem to get an idea of in what case the script is firing and when it isn't.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 25, 2024, 4:29pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/2 "2024-10-25T16:29:47Z")

</div>

You could add a custom prop on your view, with the expression `now(0)`.  
Then put your code in the value change script of this prop.

I don't like this kind of workaround, but I can't think of something else right now.

---

<div class="post-metadata">

**Author:** ![ryan.perretta](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.perretta/32/75110_2.png) [@ryan.perretta](https://forum.inductiveautomation.com/u/ryan.perretta)\
**Post date:** [October 25, 2024, 4:33pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/3 "2024-10-25T16:33:24Z")

</div>

I've definitely done that before, no judgement here 😂

If all else fails, that may be the way...

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 25, 2024, 4:49pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/4 "2024-10-25T16:49:31Z")

</div>

Wait, actually... You may encounter the very same issue. If the script runs before it can effectively put the focus on the input field...  
Try putting that script on the input field's `onStartup` event. I feel like that may be the best place to put it.

edit:  
I just tried putting `self.focus()` in the field's `onStartup` event, and it seems to work.

---

<div class="post-metadata">

**Author:** ![ryan.perretta](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.perretta/32/75110_2.png) [@ryan.perretta](https://forum.inductiveautomation.com/u/ryan.perretta)\
**Post date:** [October 25, 2024, 4:56pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/5 "2024-10-25T16:56:48Z")

</div>

I moved it to the `onStartup` event of the input field while I was troubleshooting, and I did not see any real difference. Sometimes it'd work fine, others nothing.

---

<div class="post-metadata">

**Author:** ![ryan.perretta](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.perretta/32/75110_2.png) [@ryan.perretta](https://forum.inductiveautomation.com/u/ryan.perretta)\
**Post date:** [October 25, 2024, 5:39pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/6 "2024-10-25T17:39:12Z")

</div>

My solution to this problem has been making the script sleep for a hundredth of a second before running. It will now focus 100% of the time with no discernable wait.

I won't mark this as the solution because it stinks, but it's the best I've got right now.

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [October 25, 2024, 5:42pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/7 "2024-10-25T17:42:50Z")

</div>

Setting focus onStartup is inherently flawed because it might execute before the input you're trying to focus is rendered. They really need to add an "onLoad" event handler, something I've requested and you can vote for:

> **[Perspective view onLoad event that fires when the view is done loading |...](https://inductiveautomation.canny.io/ignition-features-and-ideas/p/perspective-view-onload-event-that-fires-when-the-view-is-done-loading)**
>
> This would be the only clean way of ensuring that, for example, an input .focus() would execute at the right time... https://forum.inductiveautomation.

My solution has been to add a delay:

```python
from threading import Timer

def focus(self):
    self.focus()

Timer(1, focus, [self]).start()

```

---

<div class="post-metadata">

**Author:** ![ryan.perretta](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.perretta/32/75110_2.png) [@ryan.perretta](https://forum.inductiveautomation.com/u/ryan.perretta)\
**Post date:** [October 25, 2024, 5:46pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/8 "2024-10-25T17:46:48Z")

</div>

Thanks for the info. I wasn't really sure why that was happening, but that makes sense.

Upvoted your feature request, hopefully the Ignition gods are behind us 🤞

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [October 25, 2024, 5:50pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/9 "2024-10-25T17:50:47Z")

</div>

This issue is particularly frustrating for Ryans...

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/d/2d070ec1db0e6dad6cd9b5c27ed1c233d60be5ae.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 25, 2024, 6:02pm UTC](https://forum.inductiveautomation.com/t/execute-script-every-time-a-popup-is-opened/97780/10 "2024-10-25T18:02:56Z")

</div>

This thread gave me an idea. I use a generic popup `ConfirmWithInput` which has an input parameter of `presetValue`. This is bidirectional bound to my text input in the popup. If there is not a preset value, I want the text field to be in focus.

The `onStartUp` event was hit or miss as previously mentioned, so I added a change script on the text input which should get fired when the binding is executed.

```python
if not currentValue.value:
   self.focus()

```

So far it works every time.

```python

```
