D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] OT: Could anyone help with a bit of Javascript please?

 

I would do this with AJAX. Just use the  XMLHttpRequest. I'm currently using 
the Google GxmlHttp class, but you can roll your own easily enough (see 
http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html )

Make a function that fetches text asynchronously from the server.

function get_doc(uri, onrx) {
        var request = GXmlHttp.create();
        request.open("GET", uri, true);
        request.onreadystatechange = function() {
                if (request.readyState == 4) {
                        onrx(request.responseText);
                }
        };
        request.send(null);
}

Then get it to update an element in your HTML.

    <div id="ticker"> text here </div>

The code is:

function ticker(uri, ident, period) {
        function update(text) {
                var element = document.getElementById(ident);
                element.innerHTML = text;
                //      Do it again
                setTimeout(function() { ticker(uri, ident, update); }, period);
        }

        get_doc(uri, update);
}

// Start it going 
ticker("test.txt", "ticker", 10000);

That should avoid the screen refreshes, and will poll the server for changed 
text every 10s. You might want to add a way to kill it too.

Cheers

D

On Monday 16 June 2008 20:56:15 Rob Beard wrote:
> Hi folks,
>
> I'm currently in the process of putting together a simple(ish) web page
> to display a couple of frames and have an updating ticker box on there
> which reads the contents to display from a text file on the server.
>
> Now I've been playing around with this which does pretty much what I
> want...
>
> http://www.dynamicdrive.com/dynamicindex2/ajaxticker.htm
>
> When the page loads and the script runs, it loads the contents of the
> text file into the memory and then goes through each item one after the
> other.  However, what I want is something that will automatically update
> itself, so if for instance anything gets added to the text file that it
> picks it up when it cycles through again.
>
> At the moment to get it to do this, I've put some javascript in the main
> page with the ticker on that just refreshes the page every 60 seconds
> (refresh as if the user had pressed the F5 key).  This however causes a
> very quick flash on the screen when the page reloads which is a slight
> bit annoying.
>
> I wondered if anyone would be able to suggest (if it's possible) what
> I'd need to add to the javascript to make it automatically reload the
> text file once it has cycled through each item in the text file but
> without reloading the whole page (I just want the javascript to reload
> the text file into it's array).
>
> I'm pretty clueless when it comes to javascript.  I can understand
> little bits of it but my knowledge isn't enough to add this feature, so
> if anyone can suggest any ways of doing this then it would be great.
>
> Ta,
>
> Rob



-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html