how to make sure jquery bind event is triggered only once from document.ready function

Using jquery and javascript I do the following
I have the following piece of code in headermaster.js file, that is added to the header.master page. I get an error when I click on a link and go to a content page. my guess is that it is trying to attach the events every time a content page is loaded and throws an error in this line of code. Is it possible to do something like, bind the events only if it is not already attached.
$(document).ready(function () {
    $('#divDropMenu1').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
    $('#divDropMenu2').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
    $('#divDropMenu3').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
    $('#divDropMenu4').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
    $('#divDropMenu5').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
    $('#divDropMenu6').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
    $('#divDropMenu7').bind('mouseleave', DropMenuLeave_HideDropMenusDelayed);
});

Open in new window

TrialUserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

drainCommented:
Two questions:
a) what is the error that is occuring
b) when does it occur

I would install something like Firebug to help troubleshoot this. My initial guess would be that if the header is in some kind of (.net?) control, maybe the ID of the dropmenu is not actually what you think it is?
0
HainKurtSr. System AnalystCommented:
are you sure id is divDropMenuX
and are you sure this js function is included in teh page: DropMenuLeave_HideDropMenusDelayed

can you try this syntax

$('#divDropMenu1').mouseleave(function (){DropMenuLeave_HideDropMenusDelayed});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TrialUserAuthor Commented:
this is the error I get:
Microsoft JScript runtime error: Object expected.

The error does not occur when I open the default page. The drop downs are in the master page.
When I open a content page, the header.master gets loaded for the second time and theat is when I get the error. So the functions are working fine except when the header. master is loaded for the second time , i get the error.

the below syntax does not even trigger the function.
$('#divDropMenu1').mouseleave(function (){DropMenuLeave_HideDropMenusDelayed});
0
drainCommented:
As both Hain and I suggested, it seems like on the content pages then, the ID is not correct.
I would view source on the content pages and make sure the IDs are the same in the content pages.

in the console (in firebug or the like) does just $('#divDropMenu1') return anything
or in the browser location bar:
javascript:alert($('#divDropMenu1'))
0
TrialUserAuthor Commented:
The divDropMenu1' is the same in all content pages. In fact the first page, I open default page is a content page of the master page as well.
It seems like the second time the master page is loaded the following is giving an error:
Please suggest. Thanks

I removed all the code inside and just added an alert and still got the same error
$(document).ready(function () {
alert('hello');
}







0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.