I am a complete novice. With a lot of heart, I learnt javascript and then picked up Ajax purely from forums like these and do not have any credentials to be known as a professional coder. But still I made an attempt to build a news website and wanted to put all news items in separate htm files and call them in one single htm file using XMLHttp.
I succeeded, but the script is very inconsistent. It fetches the page sometimes and again it fails. This is what is happening in firefox but in IE the situation is more worse - it fails more often than it succeeds. I am pasting this script below for expert advice from forum members.
<!--
var xmlHttp;
function checkBrowser()
{
try
{
// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
function responseText()
{
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById("text_here").innerHTML=xmlHttp.responseText;
}
}
}
//do not disturb the below code - This code is scripted by Naveen Chand Kanyamarala. Write to
naveen@parishkaarindia.com for more details.
var currentURL=window.location;
whichnews=currentURL.search
switch (whichnews)
{
case '?intnews=1':
checkBrowser()
responseText()
xmlHttp.open("GET","intnews1.htm",true);
xmlHttp.send(null);
break
case '?intnews=2':
checkBrowser()
responseText()
xmlHttp.open("GET","intnews2.htm",true);
xmlHttp.send(null);
break
case '?intnews=3':
checkBrowser()
responseText()
xmlHttp.open("GET","intnews3.htm",true);
xmlHttp.send(null);
break
case '?intnews=4':
checkBrowser()
responseText()
xmlHttp.open("GET","intnews4.htm",true);
xmlHttp.send(null);
break
case '?intnews=5':
checkBrowser()
responseText()
xmlHttp.open("GET","intnews5.htm",true);
xmlHttp.send(null);
break
case '?natnews=1':
checkBrowser()
responseText()
xmlHttp.open("GET","natnews1.htm",true);
xmlHttp.send(null);
break
case '?natnews=2':
checkBrowser()
responseText()
xmlHttp.open("GET","natnews2.htm",true);
xmlHttp.send(null);
break
case '?natnews=3':
checkBrowser()
responseText()
xmlHttp.open("GET","natnews3.htm",true);
xmlHttp.send(null);
break
case '?natnews=4':
checkBrowser()
responseText()
xmlHttp.open("GET","natnews4.htm",true);
xmlHttp.send(null);
break
case '?natnews=5':
checkBrowser()
responseText()
xmlHttp.open("GET","natnews5.htm",true);
xmlHttp.send(null);
break
case '?regnews=1':
checkBrowser()
responseText()
xmlHttp.open("GET","regnews1.htm",true);
xmlHttp.send(null);
break
case '?regnews=2':
checkBrowser()
responseText()
xmlHttp.open("GET","regnews2.htm",true);
xmlHttp.send(null);
break
case '?regnews=3':
checkBrowser()
responseText()
xmlHttp.open("GET","regnews3.htm",true);
xmlHttp.send(null);
break
case '?regnews=4':
checkBrowser()
responseText()
xmlHttp.open("GET","regnews4.htm",true);
xmlHttp.send(null);
break
case '?regnews=5':
checkBrowser()
responseText()
xmlHttp.open("GET","regnews5.htm",true);
xmlHttp.send(null);
break
default:
document.write('<b>Welcome to News and Analysis</b>'.fontsize(3).bold())
document.write('<p>Brining you complete news along with analysis of top international, national and regional stories. Click on relevant news categories available on your right</p>')
}
//-->
Please note that there are three kinds of news - international news, national news and regional news represented as intnews, natnews and regnews. Apologies for this lengthy post.