I am struggling to use the get command in JavaScript to open a text file and populate a div tag in my HTML. The text file is located in the same folder as my html file, but I can't seem to make any progress with this task. When I click on the button I created, I want it to change the content of the div tag.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Glorious Final </title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" href="css.css" type="text/css" media="screen" charset="utf-8"/>
<script src="jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>
<script src="script.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="six-point-star"></div>
<h1>JQuery Jedi </h1>
<div id="ornamentRow1"></div>
<p>
<h2>Ajax Admiral</h2>
<div id="ornamentRow2"></div>
</p>
<h3>Javascript Janitor</h3>
<div id="custom"></div>
<h4>
<button type="button" id="twinkle" >Twinkle Me Starmo</button>
<button type="button" onclick="hangOrnament1()">Hang First Ornament</button>
<button type="button" onclick="">Hang More Ornaments</button>
</h4>
<h5>
<button type="button" onclick="">!!Open Me!!</button>
</h5>
</body>
</html>
This is the JavaScript function that I want to use from the script.js file, but I believe the issue lies in the "GET" line at the end of this code snippet:
function hangOrnament1()
{
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("ornamentRow1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ornaments.txt",true);
xmlhttp.send(null);
}
I have been researching forums for a solution for hours, but I can't find a fix for the issue. If anyone has insight into how I can correct this problem, I would greatly appreciate it.