newbie AJAX dropdown execute ONCE

Discussion in 'Javascript' started by shall@uaex.edu, Aug 5, 2010.

  1. Guest

    When a user sets focus on a textbox, I want this AJAX dropdown to run
    ONCE.
    If the user goes BACK to that same textbox, I do NOT want the
    "GetMyFile" to execute again.

    How can I do this?

    function show2Multi(aname,alist)
    {
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(alist).innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","GetMyFile.asp?f="+allYears+"&a="+aname,true);
    xmlhttp.send();
    }

    TIA
    Steve
     
    , Aug 5, 2010
    #1
    1. Advertising

  2. On 5 Aug, 16:13, wrote:
    > When a user sets focus on a textbox, I want this AJAX dropdown to run
    > ONCE.
    > If the user goes BACK to that same textbox, I do NOT want the
    > "GetMyFile" to execute again.
    >
    > How can I do this?
    >
    > function show2Multi(aname,alist)
    > {
    > if (window.XMLHttpRequest)
    >   {// code for IE7+, Firefox, Chrome, Opera, Safari
    >   xmlhttp=new XMLHttpRequest();
    >   }
    > else
    >   {// code for IE6, IE5
    >   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    >   }
    > xmlhttp.onreadystatechange=function()
    >   {
    >   if (xmlhttp.readyState==4 && xmlhttp.status==200)
    >     {
    >     document.getElementById(alist).innerHTML=xmlhttp.responseText;
    >     }
    >   }
    > xmlhttp.open("GET","GetMyFile.asp?f="+allYears+"&a="+aname,true);
    > xmlhttp.send();
    >
    > }
    >
    > TIA
    > Steve


    Remove the event trigger from the box once it loses focus.
     
    Captain Paralytic, Aug 5, 2010
    #2
    1. Advertising

  3. On 05/08/10 16:13, wrote:
    > When a user sets focus on a textbox, I want this AJAX dropdown to run
    > ONCE.
    > If the user goes BACK to that same textbox, I do NOT want the
    > "GetMyFile" to execute again.
    >
    > How can I do this?


    I'm not sure if by "textbox" you mean

    (a) textarea; or
    (b) input type="text"

    element.

    You could change the onFocus property of the element during the call to
    GetMyFile.

    eg, *IF* aname is the id of the "textbox" element:

    function GetMyFile(aname,alist)
    {
    tbox = document.getElementById(aname);
    tbox.onFocus = "";

    ..... rest of function

    }

    or, if aname is not the name of the textbox, change the onFocus handler
    like this:

    onfocus="GetMyFile('aname','alist',this)"

    Then:

    function GetMyFile(aname,alist,caller)
    {
    caller.onFocus = "";

    ..... rest of function

    }

    You may wish to add some logic to prevent the onFocus removal from
    elements of other types, or limit it to specific elements.

    Other methods include:

    Setting a global variable, eg:

    var gmfDoneOnce = false;

    function GetMyFile(aname,alist)
    {

    if (gmfDoneOnce) return;
    gmfDoneOnce = true;

    ..... rest of function

    }

    but that will mean that the function can only run once, regardles of how
    many different places you may wish to call it from.

    Rgds

    Denis McMahon
     
    Denis McMahon, Aug 5, 2010
    #3
  4. On Aug 5, 4:43 pm, Denis McMahon wrote:
    <snip>
    > eg, *IF* aname is the id of the "textbox" element:
    >
    > function GetMyFile(aname,alist)
    > {
    > tbox = document.getElementById(aname);
    > tbox.onFocus = "";
    >

    <snip>

    If the intention is to remove an - onfocus - intrinsic event listener
    function from a DOM element that it would be better if the identifier
    were spelled with all lowercase letters, and the value assigned be
    null (as some browsers (at lest IE) don't like anything but null or a
    function to be assigned to such properties).

    Richard.
     
    Richard Cornford, Aug 5, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2l2YXJhbWFuLlM=?=

    How to execute a aspx page more than once simultanously?

    =?Utf-8?B?U2l2YXJhbWFuLlM=?=, Oct 14, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    388
    John Saunders
    Oct 15, 2004
  2. PyPK
    Replies:
    9
    Views:
    417
    =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=
    Oct 16, 2005
  3. acadam
    Replies:
    0
    Views:
    766
    acadam
    Dec 27, 2006
  4. minnie
    Replies:
    1
    Views:
    726
    Andrew Thompson
    Dec 13, 2006
  5. Gancy
    Replies:
    4
    Views:
    203
    Rasto Levrinc
    Feb 3, 2005
Loading...

Share This Page