You likely want to replace window by the element containing the actual icons, in order to not have this code running when there is a dialog or stuff like this.