# Window Change Notifications

**URL:** <https://forum.inductiveautomation.com/t/window-change-notifications/11372>\
**Category:** Ignition\
**Created:** [March 17, 2016, 2:24am UTC](https://forum.inductiveautomation.com/t/window-change-notifications/11372 "2016-03-17T02:24:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [March 17, 2016, 2:24am UTC](https://forum.inductiveautomation.com/t/window-change-notifications/11372/1 "2016-03-17T02:24:33Z")

</div>

I needed a way to execute code and be notified when a window changes.

First I created a client tag change script for a value change on [System]Client/User/CurrentWindow as described [here](https://support.inductiveautomation.com/usermanuals/ignition/typical_navigation_strategy.htm?zoom_highlightsub=currentwindow).

Here is the tag change script:

`newWindowName = newValue.value
shared.nav.onWindowChanged(newWindowName)`

Then I created two client tags to keep track of the previous and current windows:

`[Client]YourProject/navigationContext/PreviousWindowName
[Client]YourProject/navigationContext/CurrentWindowName`

Then I created a script in the script library shared.nav:

[code]\_onWindowChangeCallbacks = {}

def registerOnWindowChangeCallback(name,func):  
if \_onWindowChangeCallbacks.has\_key(name):  
shared.developer.logDebug(“onWindowChange callback NOT registered: %s” % name)  
else:  
\_onWindowChangeCallbacks[name] = func  
shared.developer.logDebug(“onWindowChange callback registered: %s” % name)  
shared.developer.logDebug(“onWindowChange callback count: %s” % len(\_onWindowChangeCallbacks))

def unregisterOnWindowChangeCallback(name):  
if \_onWindowChangeCallbacks.has\_key(name):  
shared.developer.logDebug(“onWindowChange callback unregistered: %s” % name)  
del \_onWindowChangeCallbacks[name]  
shared.developer.logDebug(“onWindowChange callback count: %s” % len(\_onWindowChangeCallbacks))

def \_executeOnWindowChangeCallbacks():  
unregisters = []  
for key,value in \_onWindowChangeCallbacks.iteritems():  
result = value(key,getPreviousWindowName(),getCurrentWindowName())  
if(result == True):  
unregisters.append(key)

```
for r in unregisters:
	shared.developer.logDebug("onWindowChange callback self-unregister request: %s" % r)
	unregisterOnWindowChangeCallback(r)	

```

def getPreviousWindowName():  
return system.tag.read("[Client]YourProject/navigationContext/PreviousWindowName").value

def setPreviousWindowName(name):  
system.tag.write("[Client]YourProject/navigationContext/PreviousWindowName", name)

def getCurrentWindowName():  
return system.tag.read("[Client]YourProject/navigationContext/CurrentWindowName").value

def setCurrentWindowName(name):  
system.tag.write("[Client]YourProject/navigationContext/CurrentWindowName", name)

def onWindowChanged(newWindowName):  
if(newWindowName):  
previousWindowName = getCurrentWindowName()  
setPreviousWindowName(previousWindowName)  
setCurrentWindowName(newWindowName)  
shared.developer.logDebug(“onWindowChange:: ‘%s’ =\> ‘%s’.” % (previousWindowName, newWindowName))  
\_executeOnWindowChangeCallbacks()  
return True  
else:  
return False[/code]

The above script now allows you to register functions for invocation when the current window changes:

Register Function in visionWindowOpened:

```auto
shared.nav.registerOnWindowChangeCallback("myCallback",event.source.newMethod)
```

Unregister Function in visionWindowClosed:

```auto
shared.nav.unregisterOnWindowChangeCallback("myCallback")
```

And then define the “newMethod” as a custom method on the window:

`
def newMethod(self,a,b,c)
     print "callback:::: " + a + " : " + b + " => " + c
     return True`

NOTE: returning False would prevent the callback from unregistering itself until unregisterOnWindowChangeCallback is invoked.

Sample output:

`onWindowChange callback registered: myCallback
onWindowChange callback count: 1
onWindowChange:: 'authentication/Kronos' => 'navigation/Landing'.
callback:::: myCallback : authentication/Kronos => navigation/Landing
onWindowChange callback self-unregister request: myCallback
onWindowChange callback unregistered: myCallback
onWindowChange callback count: 0`
