ShadowIce
06-23-2009, 03:04 PM
Hi all. I've been studying ajax for a while now. The problem is it works great in IE, but not in firefox 5+ and opera..
I would like for this code to check if ajax exists and if it does, then read and return text to a div.. even if its in ff..
Here's the code:
<html>
<body>
<script type="text/javascript">
function readdata(news,nfile){
var txtFile = new XMLHttpRequest();
txtFile.open("GET", nfile, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
document.getElementById(news).innerHTML = allText;
} else {
document.getElementById(news).innerHTML = 'News file not found';
}
}
}
txtFile.send();
}
function ajax(news,nfile){
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
}
</script>
<div id="news"><script type="text/javascript">ajax('news','news.txt');</script></div>
<form name="myForm">
Name: <input type="text" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>
You will need to make news.txt in same location for this to work.
ANY help is GREATLY appreciated! :)
~SI~
I would like for this code to check if ajax exists and if it does, then read and return text to a div.. even if its in ff..
Here's the code:
<html>
<body>
<script type="text/javascript">
function readdata(news,nfile){
var txtFile = new XMLHttpRequest();
txtFile.open("GET", nfile, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
document.getElementById(news).innerHTML = allText;
} else {
document.getElementById(news).innerHTML = 'News file not found';
}
}
}
txtFile.send();
}
function ajax(news,nfile){
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
}
</script>
<div id="news"><script type="text/javascript">ajax('news','news.txt');</script></div>
<form name="myForm">
Name: <input type="text" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>
You will need to make news.txt in same location for this to work.
ANY help is GREATLY appreciated! :)
~SI~