jQuery onfocus double fire problem

Posted on 2013-05-12
Medium Priority
Last Modified: 2013-05-12

I am trying to track the amount of times someone goes to a webpage then leaves the tab and then goes back to it but I am having an issue with the jquery double firing.

Here is my code:

<script type="text/javascript">
var f = 1;
	console.log("window clicked on " + f + " times")

Open in new window

The console looks like this (my actions are in italics):
click to foucs
window clicked on 2 times
window clicked on 3 times
click off the tab then click back
window clicked on 4 times
window clicked on 5 times
click off the tab then click back
window clicked on 6 times
window clicked on 7 times

Does anyone know how to stop the double fire?


Question by:NicoM79
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 58

Expert Comment

ID: 39160060
Checked in FF and IE10 and it only fires once.

Author Comment

ID: 39160069
Hi GaryC123,

Sorry if I wasn't clear. I don't mind that it starting at two.

To show you what I mean i recorded a quick 18 sec screencast video

This is what I did:

I open up a test html page with the script on it then tab to the next tab a W3 school page and go back to my original test page. Each time I leave the page to go to the next tab and go back it puts two new console.log statements.

Let me know if that makes sense.


LVL 58

Accepted Solution

Gary earned 2000 total points
ID: 39160086
Must be a Chrome bug because it works fine in IE and FF, amended your code to fix it.

var f = 1;
var gotFocus = false;  

	gotFocus = true;

	console.log("window clicked on " + f + " times")
$(window).blur(function() {
	gotFocus = false;

Open in new window


Author Closing Comment

ID: 39160094
Thanks a lot!

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question