Sample AJAX News Module
var currentTab = "news0_title";
var tabCount = 0;
var tabTrack = [];
var currentCategory;
var notRounded = 0;
var image = "../images/news_main_bg.jpg";
//var currentNews = '';
var littleNews = [];
var itemTab
littleNews[0] = 2;
littleNews[1] = 3;
littleNews[2] = 4;
document.getElementsByClassName = function(name) {
var results = [];
var elems = document.getElementsByTagName("*");
for (var i=0; i<elems.length; i++) {
if (elems[i].className.indexOf(name) != -1) {
results[results.length] = elems[i];
}
}
return results;
};
function makeRequest(url, itemID) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
/*alert('Giving up :( Cannot create an XMLHTTP instance');*/
return false;
}
http_request.onreadystatechange = function() { showContents(http_request, itemID); };
http_request.open('GET', url, true);
http_request.send(null);
}
function showContents(http_request, itemID) {
/*alert(http_request.responseText);*/
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById("newscontent").innerHTML = http_request.responseText;
//Track tabs and itemID
//tabTrack[tabcount] = itemID;
//tabCount++;
/*alert(http_request.responseText.toString());
alert(currentCategory);*/
contentArr[currentCategory] = http_request.responseText;
/*if(contentArr[currentCategory].length < 500){
document.getElementById(itemID).style.display = "none";
document.getElementById("news0_title").focus();
}
else{
//roundNewsImages();
newsBgColor(99);
}*/
newsBgColor(99);
} else {
/*alert('There was a problem with the request.');*/
}
}
}
function getNews(itemID,languageID,categoryid,overviewID) {
try {
if (contentArr[categoryid] == '') { // if not previously loaded
document.getElementById(currentTab).className = "newstitle";
document.getElementById(itemID).className = "newstitle_on";
makeRequest(thisURL+"categoryid="+categoryid+"&languageid="+languageID+"&overviewID="+overviewID,itemID);
currentTab = itemID;
//alert(itemID);
urchinTracker("ajaxnews/loadPage/categoryid="+categoryid+"/langid="+languageID+"/overviewID="+overviewID,itemID);
}
else {
document.getElementById(currentTab).className = "newstitle";
document.getElementById(itemID).className = "newstitle_on";
document.getElementById("newscontent").innerHTML = contentArr[categoryid];
//alert(itemID);
currentTab = itemID;
//roundNewsImages();
newsBgColor(99);
}
}
catch (e) {
}
}
function newsBgColor(curStory){
var i;
var littleDivID;
if(document.getElementById("newsStories_main") )
{
document.getElementById("newsStories_main").style.background="url(" + image + ") repeat-y scroll bottom right";
document.getElementById("newsStories_main").style.cursor="pointer";
document.getElementById("newsStories_main").onmouseover = function(){
this.style.background="#dedede";
this.getElementsByTagName("h3")[0].style.color="maroon";
}
document.getElementById("newsStories_main").onmouseout = function(){
this.style.background="url(" + image + ") repeat-y scroll bottom right";
this.getElementsByTagName("h3")[0].style.color="#3F4446";
}
}
for (i = -1; i < 3; i++){
littleDivID = ["newsStories_little" + i];
if(document.getElementById(littleDivID) )
{
/* Change div background color for hover events */
if (window.addEventListener)
{
document.getElementById(littleDivID).addEventListener("click",function(){this.style.backgroundColor="#efefef";},false);
document.getElementById(littleDivID).addEventListener("mouseout",function(){this.style.backgroundColor="transparent";},false);
document.getElementById(littleDivID).addEventListener("mouseover",function(){this.style.backgroundColor="#efefef";},false);
/* Reset background color to the default for the currently selected div */
if (i == curStory){
document.getElementById(littleDivID).addEventListener("mouseout",function(){this.style.backgroundColor="#efefef";},false);
}
else{
document.getElementById(littleDivID).style.backgroundColor="transparent";
}
}
/* Alternate version of same function for IE */
else
{
document.getElementById(littleDivID).onclick=function(){this.style.backgroundColor="#efefef";}
document.getElementById(littleDivID).onmouseout=function(){this.style.backgroundColor="transparent";}
document.getElementById(littleDivID).onmouseover=function(){this.style.backgroundColor="#efefef";}
if (i == curStory){
document.getElementById(littleDivID).onmouseout=function(){this.style.backgroundColor="#efefef";}
}
else{
document.getElementById(littleDivID).style.backgroundColor="transparent";
}
}
}
}
}
function showStory(articlenum) {
var divID = ["newsStory_hidden" + articlenum];
var tmpID, currentHTML;
var currentNews = parseInt(articlenum) - 2;
var littleDivID = ["newsStories_little" + currentNews];
var langID = getParameter('langid');
var crop;
var croppedText;
var more = /\[More\]/;
var moreStr = " [More]";
var moreText = moreStr.fontcolor("red");
var para = /\r/;
var cut;
var clickedStory;
var i;
var j;
newsBgColor(currentNews);
/*oldMain_little= document.getElementById("newsStories_little_hidden"+currentNews).innerHTML;
for (var i = 0; i < littleNews.length; i++) {
if (littleNews[i] == articlenum) {
tmpID = "newsStories_little" + i;
document.getElementById(tmpID).innerHTML = oldMain_little;
littleNews[i] = currentNews;
}
}
switch (langID) {
case "41":
crop = 200;
break;
case "5":
crop = 175;
default:
crop = document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeValue.length;
break;
}
croppedText = document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeValue.substring(0,crop);
if (!more.test(croppedText) )
{croppedText = croppedText.concat("...");}
document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeValue = croppedText;
*/
/* Copy the node containing main headline text for the selected tab */
croppedText = document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeValue;
/* If the copied node is actually an extra br tag, delete it and get the node above it instead */
if (document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeType == 1)
{
document.getElementById(divID).getElementsByTagName("a")[2].removeChild(document.getElementById(divID).getElementsByTagName("a")[2].lastChild);
croppedText = document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeValue;
}
/* Crop text after the first sentence if the first paragraph is one sentence long */
if(para.test(croppedText) )
{
cut = croppedText.indexOf(".");
croppedText = croppedText.slice(0,cut);
}
/* Add ellipsis to the text if there aren't any already there */
/*if (!more.test(croppedText) && croppedText.length > 20)
{croppedText = croppedText.concat(" [more]");}
*/
/* Replace the default text with the altered string */
document.getElementById(divID).getElementsByTagName("a")[2].lastChild.nodeValue = croppedText;
document.getElementById("newsStories_main").innerHTML = document.getElementById(divID).innerHTML;
/*for (var i = 0; i < 4; i++)
{
var j = i - 1;
littleDivID = ["newsStories_little" + j];
if (j == currentNews)
{document.getElementById(littleDivID).getElementsByClassName("newsListText")[0].style.backgroundColor="#efefef";}
else
{document.getElementById(littleDivID).getElementsByClassName("newsListText")[0].style.backgroundColor="transparent";}
}
currentNews = articlenum;*/
}
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.