...

View Full Version : Reading a txt or xml file



Pezmc
06-12-2008, 12:15 AM
Is there any short and simple way I can get my JavaScript app to read a xml or txt file containing just something like 1.5.2 and compare it to variable version number var VersionNumber = "1.4.1"; then if it is a old version run a function? I have absolutely no idea how you would do this but it needs to be short and simple.

Pez,

rnd me
06-12-2008, 12:45 AM
function IO(U, V) {// A tiny ajax library. 2007, DanDavis
var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
X.open(V ? 'PUT' : 'GET', U, !1);
X.setRequestHeader('Content-Type', 'text/html')
X.send(V ? V : '');
return X.responseText;}

function runMe(){
alert ("running the update function");
return true;
}



// ------------------------ customize below

var oldVersion = "1.4.1";
var newVersion = IO("text.txt");
if( oldVersion != newVersion ){ runMe(); }

Pezmc
06-12-2008, 12:58 AM
Would this work in firefox? It seems to be using the activeX protocol.

rnd me
06-12-2008, 01:24 AM
Would this work in firefox? It seems to be using the activeX protocol.

tested and working in ie 6 and 7, ff2 and 3, opera 9, and safari 3.

i bet it works in others as well.

Pezmc
06-12-2008, 02:04 AM
Thanks for your help. It is really useful! Is there anyway to get this to read data on another site as the js is for greasemonkey so is locally stored and my server txt file is at:
http://ikariamsolarium.googlecode.com/svn/trunk/version.txt

rnd me
06-12-2008, 05:21 AM
not with the code posted, but here is greasemonkey version

in html page:

function gmCallBack(a){
var oldVersion = "1.4.1";
var newVersion = a;
if( oldVersion != newVersion ){ runMe(); }
}

gmCallBack.url = "http://ikariamsolarium.googlecode.com/svn/trunk/version.txt"



in user script:


(function (callBack){ //one-way io (read) for grease monkey; by dandavis
if(!callBack){return;}
GM_xmlhttpRequest({ method: 'GET',url: callBack.url, headers: {
'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14',
'Accept': 'text/plain,text/html,text/xml'},
onload: function(a){ callBack(a.responseText); } });
return "data arrives as argument0 to window.gmCallBack";
}( unsafeWindow.gmCallBack )) //change this var to fetch multiple urls



this works only once per page load, so copy the code to get multiple pages.
you can cut and paste several copies of the user script code block in your script.
change only the parenthesized text on the last line to a different html-page callback function name.

Pezmc
06-12-2008, 10:13 AM
You just earned youself three thanks! You deserve it, this is now a critical part of my greasemonkey script. Would you like a
//By Rnd_Me
included before these functions as i would gladly include it!

Edit: Where does the "in html page" stuff go?
How do I actually start the function?

Once again thanks!

Pezmc
06-12-2008, 05:31 PM
Does anyone else know?

Pezmc
06-12-2008, 08:40 PM
Bump: Sorry but I don't want this thread to die

rnd me
06-12-2008, 11:33 PM
Edit: Where does the "in html page" stuff go?
How do I actually start the function?

1
in a script section, alongside your other code.
directly after function runMe for instance...


2
you do not need to start the function.
It is called by greaseMonkey when the remote URL's data arrives.




glad to hear things are working well for you.

I'll gladly take a "comment credit" in any code, thanks.

barkermn01
06-13-2008, 01:19 PM
dont lol use php it is easyer
faster and alot less text just google it there are loads of tutorials



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum