/    Sign up×
Community /Pin to ProfileBookmark

One keydown event multiple actions problem.

We can have one event attached to multiple actions in javascript. I have facing problem in this scenario.

  • 1. I am having first.jsp, second.jsp

  • 2. Both pages load one.js and two.js
    3.In one.js, I am attaching document.attachEvent(“onkeydown”, functionOne).
    4.In two.js, I am attaching document.attachEvent(“onkeydown”, functionTwo).
  • Now I am loading page first.jsp. In first.jsp i have a button when i click on it second.jsp is loaded as popup window, that means first.jsp is parent and second.jsp is popup and both have one.js and two.js loaded simultaneously and events attached.

    Problem: (browser IE 6)
    In parent (first.jsp), when I press a key, functionTwo is executed first and then functionOne.
    In popup(second.jsp), when I press a key, functionOne is executed first and then functionTwo.

    Questions:
    1. when a event is attached to multiple functions then what is the priority by which they are called?
    2.In the above problem, why the functions are executing reverse? Which is the correct scenario? how to rectify the problem?

    I appreciate if some one can answer the above questions.

    to post a comment
    JavaScript

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @FangJan 14.2009 — http://msdn.microsoft.com/en-us/library/ms536343(VS.85).aspx
    If you attach multiple functions to the same event on the same object, the functions are called in random order[/QUOTE]
    Copy linkTweet thisAlerts:
    @sekhar_generalauthorJan 15.2009 — Thank you Feng for the reply. I have gone through the msdn link provided.

    Now I understand that the functions for the same event are fired in random. So, is there a way if I can make them sequence? I mean, is there a solution for this problem?
    Copy linkTweet thisAlerts:
    @FangJan 15.2009 — I would attach 1 event which calls the 2 functions in the correct order.
    Copy linkTweet thisAlerts:
    @sekhar_generalauthorJan 29.2009 — Thanks Fang, we have achieved the way you have explained. We are attaching only one function and internally calling other functions. Thank you for the help.
    ×

    Success!

    Help @sekhar_general spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...