-1

Hi i did some search about some function that i need , and i found it here on StackOverFlow . The problem is in all browsers i get "bug" on the script that says :

mouse_is_inside is not defined

But its defined ,and the function working perfect, and can't get rid from that message any advance?

I tryed though of that . but i dont know how to assume :

if (typeof variable === 'undefined') {
    // variable is undefined
}

My function :

$(document).ready(function()
{
    $('#contactbox').hover(function(){ 
        mouse_is_inside=true; 
    }, function(){ 
        mouse_is_inside=false; 
    });

    $("body").mouseup(function(){

        if(! mouse_is_inside)  // the problem is here says not defined .
            {
            if ($.browser.msie  && parseInt($.browser.version, 10) === 7) {
                   $("#main").css('z-index','0');
                  } 
                  $('#contactb a').removeClass('cactive');
             $('#contactb a').addClass('cnoactive');
            $('#contactbox').hide()
            }
    });
});

EDIT thanks my bad , didnt know its so easy . i will tick the answer.

0

2 Answers 2

1

All you need is to add this line:

var mouse_is_inside = false;

...just inside the function. E.g.:

$(document).ready(function()
{
    var mouse_is_inside = false;
    // ...the rest of the code
});

Because the error is correct: You haven't defined it anywhere. But then the first time you do this:

mouse_is_inside=false;

or this:

mouse_is_inside=true;

...you're defining it, by falling prey to The Horror of Implicit Globals. In JavaScript's "loose" mode (the default), if you try to read the value of an undefined symbol, it's a ReferenceError; but if you write to an undefined symbol, it creates a global variable (implicitly). Fortunately, as of ES5, we have "strict" mode which makes both operations the errors they should be.

Sign up to request clarification or add additional context in comments.

3 Comments

But the variable it's global isn't it, sorry to doubt but I noticed that
@PlaceUserNameHere: Yes, it is -- it's an implicit global, because it's not defined anywhere, but the OP is assigning to it.
@Ravg: Easy mistake to make. Check out strict mode, it's really useful to catch things like this (or typos, e.g., you declare the variable blarg but type blagr by mistake).
0

Add declaration of mouse_is_inside i.e.

$(document).ready(function () {
    var mouse_is_inside;
    $('#contactbox').hover(function () {
        mouse_is_inside = true;
    }, function () {
        mouse_is_inside = false;
    });

    $("body").mouseup(function () {

        if (!mouse_is_inside) // the problem is here says not defined .
        {
            if ($.browser.msie && parseInt($.browser.version, 10) === 7) {
                $("#main").css('z-index', '0');
            }
            $('#contactb a').removeClass('cactive');
            $('#contactb a').addClass('cnoactive');
            $('#contactbox').hide()
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.