News ticker based off of an RSS

This script reads an RSS file via aj_get_alerts.php which takes as an argument the item number from the RSS. It returns an item formatted for display on the web. The JavaScript is recursive, so it runs ws_show_alert() which runs ws_get_alerts() and updates the division with the output from aj_get_alerts.php. After that, it ups the alert number by 1 and then runs setTimeout() on ws_show_alert(). This is where the recursiveness kicks in and will repeat. aj_get_alerts.php performs a modulus on the alert number based on the number of alerts or items in the RSS. So if there are 3 alerts, the number 0, 1 and 2 correspond to them. As soon as we get to alert number 3, there isn't an alert, but since there are 3 items, we do a mod 3 on the current alert number. so 3 mod 3 = 0 and 4 mod 3 = 1 and 5 mod 3 = 2 so you can see the items will rotate infinitely.

<html>
<head>
<title>News Ticker</title>
<script type="text/javascript">
function ws_http_request()
{
    var http;
    try
    {
        // Firefox ...
        http = new XMLHttpRequest();
    }
    catch(e)
    {
        // IE
        try
        {
            http = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                http = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                alert("Ajax not supported.");
                return false;
            }
        }
    }

    return http;
}

function ws_ticker(some_id)
{
    ws_show_alert(some_id,0);
}

function ws_get_alerts(some_id,alert_number)
{
    http = ws_http_request();

    http.onreadystatechange=function()
    {
        if(http.readyState == 4)
        {
            document.getElementById(some_id).innerHTML = http.responseText;
        }
        if(http.readyState == 1)
        {
            document.getElementById(some_id).innerHTML = "Still Loading...";
        }
    }
    http.open("POST","aj_get_alerts.php?alert_number=" + alert_number,true);
    http.send(null);
}

function ws_show_alert(some_id,alert_number)
{
    ws_get_alerts(some_id,alert_number);
    alert_number += 1;
    setTimeout("ws_show_alert('" + some_id + "'," + alert_number + ")",5000);
}
</script>
</head>
<body onLoad="ws_ticker('alerts')">
<div id="alerts"></div>
</body>
</html>

Page Comments (Click to edit)

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Powered by pmwiki-2.2.0-beta65