views:

2187

answers:

1

I need to use JQuery like the follwoing:

var focusFlag = 1;
jQuery(window).bind("focus", function(event)
{
focusFlag = 1;
});

jQuery(window).bind("blur", function(event)
{
focusFlag = 0;
});

Does anyone know why this doesn't work for IE?

A: 

What karim79 is saying is that hardly any jQuery code works, if the DOM is not ready, you can't be sure that the window object even exists.

jQuery(document).ready(function ()
{
    // When the page has been loaded, this function will fire.
    // NOW you can bind your events. Like so:

    jQuery(window).bind("focus", function(event)
    {
        // logic goes here
    });    
});

Check out the documentation for for the ready event.


Here's your code implemented correctly:

// You can put your focusFlag variable inside the ready event's anonymous
// function, but then it will only be accessible within the scope of that
// function (my guess is that this is not what you want).

var focusFlag = 1;

jQuery(document).ready(function ()
{
    jQuery(window).bind("focus", function(event)
    {
        focusFlag = 1;

    }).bind("blur", function(event)
    {
        focusFlag = 0;
    });
});

One of the major advantages of using jQuery is to have your code working properly, cross-browser.. Without having to implement random snippets of bathroom wall code.

roosteronacid
The thing is... what you call 'code implemented correctly' and 'cross-browser' doesn't work in internet explorer... Did you test it before posting it?
Anthony