how to recognize foregound or background state of application

Using visibilitychange event, we can recognize foreground or background state of application.