Fix emit events on 'window' and 'document' objects

This pull request fixes #17332.