combining both function help needed?

1 replies
function showUser25(str1)
{
if (str1=="")
{
document.getElementById("words").innerHTML="";
return;
}
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("words").innerHTML=xmlhttp .responseText;
}
}
xmlhttp.open("GET","wordCount.php?p="+str1,true);
xmlhttp.send();
}
function showUser26(str1)
{
if (str1=="")
{
document.getElementById("orderprice").innerHTML="" ;
return;
}
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("orderprice").innerHTML=xm lhttp.responseText;
}
}
xmlhttp.open("GET","wordCalculate.php?p="+str1,tru e);
xmlhttp.send();
}

I want to combine both these functions please help so that i can call only one function at onchange or onKeyUp
#call #function #onchange #onkeyup
  • Profile picture of the author Daniel Santos
    You should be able to call both functions from a single event (example: onKeyUp="showUser25(str1);showUser26(str1);"). The only thing you would have to do is change the xmlhttp variables within your if...else statements to make them unique, otherwise the second function would overwrite the first (because you've used automatic global variables). For example, the showUser25 function might use xmlhttp25 and the showUser26 function might use xmlhttp26.

    Update:
    I should've probably mentioned that, unless global variables are an absolute necessity, it might be wiser to use local variables.
    {{ DiscussionBoard.errors[10105159].message }}

Trending Topics