I have the following code executed on window load:

var focuscheck = setInterval(focuswindow, 1000);

$(anotherwindow).focus(function()
{
    focuscheck = setInterval(focuswindow, 1000);
});

$(anotherwindow).blur(function()
{
    clearInterval(focuscheck);

    $("iframe:first", anotherwindow.document).on("mouseover", function(event)
    {
        focuscheck = setInterval(focuswindow, 1000);
    });
});

anotherwindow and focuswindow are fine so I won't post code for that. So this is how the code works, on window load, it starts a setinterval to function focuswindow every second. As long as that window is focused, it should keep the setinterval working. When the window is blurred (moved away), setinterval should stop working until the window comes back into focus. Now, I have an iframe, which is not the same domain, so jQuery won't do certain functions. The problem is, when I click on the iframe, the code detects it as a blur (which I don't want because the iframe is in the same window so setinterval should still keep working). However, jQuery does not allow the use of .on("click") because of the same origin policy so I'm trying to "fake" a click by saying if the window is blurred but user actually clicked the iframe window (meaning cursor is on the iframe) then still keep setinterval working. The main problem is that this does not work on window load the first time. Only way it works is to purposely blur the window by switching to another tab, then go back to the window then the iframe click will work. Is there a way to fix it and if possible, even simplify the code? Thanks.

Member Avatar for LastMitch

However, jQuery does not allow the use of .on("click") because of the same origin policy so I'm trying to "fake" a click by saying if the window is blurred but user actually clicked the iframe window (meaning cursor is on the iframe) then still keep setinterval working. The main problem is that this does not work on window load the first time. Only way it works is to purposely blur the window by switching to another tab, then go back to the window then the iframe click will work. Is there a way to fix it and if possible, even simplify the code?

@secretply

I got lost somewhere in your paragraph. Are you using the iframes for ads?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.