views:

33

answers:

1

I am working on the jsp/servlet/ajax application. I use XMLHttpRequest to pass values from the jsp page to servlet, which retrieve data from the database and returns xml to the jsp.

The code works but there is one thing I do not understand. Here is an JSP part

  <body>
     <label>Longitude</label><input type="text" id ="lat" value="40.799559" />
    <br />
    <label>Latitude</label><input type="text" id="lon" value="-74.481386" />
    <br />
    <br />
    <input type="button" onclick="checkGPSCoords(document)" value="Test" />
    <br/><br/>
    <input type="text" id ="dbCounty" readonly/>
    <br/>
    <input type="text" id ="dbMuni" readonly />
    <br/>
    <br />
   </body>

I am passing a document element into the JavaScript Here is a script:

<script type="text/javascript" language="JavaScript">
        var req;
        var isIE;
        function initRequest(){
            if (window.XMLHttpRequest) {
                req = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                isIE = true;
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }

        function checkGPSCoords(currentWindow){
            var lat= currentWindow.getElementById("lat").value;
            var lon = document.getElementById("lon").value;
            //alert("lon:" + lon);
            initRequest();
            req.open("GET","./lonlat?lat="+ lat + "&lon=" + lon);
            req.onreadystatechange = retrieveMuniCntyNames;
            req.send(null);
        }

        function retrieveMuniCntyNames()
        {
           var muniAndCnty;

           if (req.readyState==4)
           {
               if(req.status==200)
               {
              var XMLresult = req.responseXML;
               muniAndCnty = XMLresult.getElementsByTagName("rec");

              //incoming from Servlet  <twp><rec twp='Morristown town' cnty='Morris' /></twp>
              var c = document.getElementById("dbCounty");
              var t = document.getElementById("dbMuni")
              c.setAttribute("value",muniAndCnty[0].getAttribute("cnty") )
              t.setAttribute("value",muniAndCnty[0].getAttribute("municipality") )                        
              }  }}</script>

Function checkGPSCoords knows a document name (which is my jsp file name). What I puzzles me that callback function retrieveMuniCntyNames() also knows the name of the document since it sets attributes to input elements on the jsp without error. I checked it the firebug.

I would appreciate any thoughts on the subject. Thanks, Chris

+1  A: 

I'm unsure what setup you're trying to convey here, or where the javascript would be in the code. I'm making the assumption here that you've defined the script in the same page or you're including it from an external file.

Nonetheless, when the javascript executes, the browser isn't leaving the page. Therefore, the document doesn't change. From the code you've provided, you're not attempting to open a new window or navigate away from the page. Therefore the document variable hasn't changed from one function call to another, even after a successful AJAX call. It's the same document before, during, and after the AJAX call.

By that same token, you can probably eliminate the currentWindow parameter from your checkGPSCoords function. You're not really checking a window object since you're passing a document object to the function. Also, the window object doesn't have a getElementById method.

villecoder
Yes, your are correct. JS is in a separate file that is why I was not sure about the document. But now I understand, a browser does not leave a page; so currentWindow parameter is unnecessary.Thanks
Greener