by aherrick
8. August 2008 07:08
Today I was working on some code to monitor my network speed (incoming and outgoing). I wrote a windows service to capture my bandwidth and dynamically create an xml file and upload it to my website. Every 30 seconds (time is totally configurable) I override the exisiting XML with an XML containing a fresh capture of my bandwidth speed. How I wote the service is another post for another day... However I do want to share some code I wrote to load the XML and update the text.
Here is an example of the XML file my windows service generates.
The XML file is named bandwidth.xml
I wrote a javascript function to attempt to read the XML file when it is in the same directory as my html file. See javascript function parseXML below.
At the end of the function I make use of setTimeout to call the function again the function after a period of time. In this case every 10 seconds.
I load the Javascript function using the body onload event.
body onload="parseXML()"
I have three span tags in my html. XML, upload, and download. I grab their IDs in the function and use innerHTML to replace the text. Try it out by loading the html in a browser and changing the xml values. Have some fun with it!
Check out the XML and HTML below to see the full code. Good luck!
bandwidth.xml (100.00 bytes)
index.html (1.43 kb)