Search code examples
javajavascriptajaxstruts-1

How to call two java script in onchange simultaneously?


I am calling two java script in onChange which are called two different action of struts.

Code follows:

<html:select property="countryid" onchange="retrieveURL('showStates.do?country=' + this.value);retrieveURL2('showStatesNotinGroup.do?country=' + this.value);">



     function retrieveURL(url)
   {
    if(window.XMLHttpRequest)
    {
        // Non-IE browsers
        req = new XMLHttpRequest();       
        req.onreadystatechange = processStateChange;
        try {
             req.open("GET", url, true);
        } catch (e) {
             alert(e);
        }
        req.send(null);
    } else if (window.ActiveXObject) { // IE

         req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
             req.onreadystatechange = processStateChange;
             req.open("GET", url, true);
             req.send();
         }
    }

   }
   function processStateChange() {
        if (req.readyState == 4) { // Complete
        if (req.status == 200) { // OK response

                document.getElementById("box2View").innerHTML = req.responseText;
            } else {
             alert("Problem: " + req.statusText);
            }
        }
    }

function retrieveURL2(url)
    {
        if (window.XMLHttpRequest) {
            // Non_IE broeser
            req = new XMLHttpRequest();
            req.onreadystatechange = processCityChange;
            try {
                req.open("GET", url, true);
            } catch (e) {
                alert(e);
            }
            req.send(null);         
        }  else if (window.ActiveXObject) {
            //IE
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = processCityChange;
                req.open("GET", url, true);
                req.send();
            }
        }       
   }

   function processCityChange(){
    if (req.readyState == 4) { //Coplete
        if (req.status == 200) { // OK responce
            document.getElementById("box1View").innerHTML = req.responseText;
        }else {
            alert("Problem: " + req.statusText);
        }
    }   
   }

For this action mapping is:

<action path="/showStates" type="com.dss.action.ShowStatesAction" validate="false" name="stateForm">
            <forward name="success" path="/showStates.jsp"/>
        </action>       
        <action path="/showStatesNotinGroup" type="com.dss.action.ShowStatesAction" validate="false" name="stateForm">
            <forward name="success" path="/showStatesNotInGroup.jsp"/>
        </action>       
    </action-mappings>

When I run it one by one for checking it works fine, but when I call it together it's giving me an unexpected result.

I want to call first java script and check whether it's successful and then call the second one on same onChange.


Solution

  • i found the way to do it and its work fine so i share you my way

    <html:select property="countryid" onchange="retrieveURL(this.value);">
    

    and the java script is like

    function retrieveURL(url){
    
    var newUrl = 'showStates.do?country='+url; 
    
    // do some thing
    
    
    retrieveURL2(url);
    
    }
    
    function retrieveURL2(url){
    
    var newUrl2 = 'showStatesNotinGroup.do?country='+url; 
    // do same thing
    
    }