I cant seem to get a very simple ajax script to work. All I want to do is change the value of text with text contained in a local text file. This is what I have:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
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)
{
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "coordinates.txt", true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
In the coordinates.txt file I have this:
<p>Test Coords</p>
When the button is clicked the text on the page should change to Test Coords, but for some reason it just clears the old text but doesn't insert the new text. Can anyone tell me what I'm doing wrong? This seems pretty simple and straight forward but for some reason will not work.