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