newbie AJAX dropdown execute ONCE

S

shall

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
 
C

Captain Paralytic

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.
 
D

Denis McMahon

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
 
R

Richard Cornford

On Aug 5, 4:43 pm, Denis McMahon wrote:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top