/    Sign up×
Community /Pin to ProfileBookmark

do an event when value = 1

Hello, this is perhaps an easy question but i would like to know how a javascript can read text from a textfile (wich only contains the number 1 or number 2) When its 1 then the script doesnt need to do anything but as soon as its “2” then i want it to reload the page. So basicly what it needs to do is that it should keep checking if the number is changed. How can i do this?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@7studJan 24.2005 — Where will the text file be located?

1) If the textfile is on the user's computer, then you can't read from it.

2) If the textfile is on the server, then you can read from it by sending out an XMLHttpRequest, which is advanced javascript. You can read about it here:

http://jibbering.com/2002/4/httprequest.html#HEAD
Copy linkTweet thisAlerts:
@AdamGundryJan 24.2005 — Here's some code using XMLHttpRequest, which requires the file to be on the same server as the calling page. (This has not been thoroughly tested.)

<script type="text/javascript">
var interval = 5000;
var url = 'data.txt';

var request = (typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() : (typeof ActiveXObject != 'undefined' ? new ActiveXObject('Microsoft.XMLHTTP') : false));

if (request){
request.onreadystatechange = dataReceived;
setInterval('checkFile()', interval);
}

function checkFile(){
if ((request.readyState == 0) || (request.readyState == 4)){
request.open('GET', url, true);
request.send(null);
}
}

function dataReceived(){
if ((request.readyState == 4) && (request.status >= 200) && (request.status < 300) && (request.responseText == '2')){
location.reload();
}
}
</script>


Set interval to the update frequency (in milliseconds, so 5000 is every 5 seconds or so), and url to the name of the file. You can use a http:// address, but it will probably only work on the same server.

Adam
Copy linkTweet thisAlerts:
@nofxauthorJan 24.2005 — i tried it but it didnt work. A php script replaced the number "1" in the textfile "data.txt" with the number "2", now the javascript should act right? But it didnt seem to do anything.

Im not sure about this line but should i edit some part of it?

var request = (typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() : (typeof ActiveXObject != 'undefined' ? new ActiveXObject('Microsoft.XMLHTTP') : false));
Copy linkTweet thisAlerts:
@7studJan 24.2005 — That line is a 'cross browser' feature. What browser are you using?
Copy linkTweet thisAlerts:
@nofxauthorJan 24.2005 — im using IE explorer. But the people who visit my site could be using opera, modzilla or firefox or something. Isnt this compatible with all browsers?
Copy linkTweet thisAlerts:
@CharlesJan 24.2005 — [i]Originally posted by nofx [/i]

[B]im using IE explorer. But the people who visit my site could be using opera, modzilla or firefox or something. Isnt this compatible with all browsers? [/B][/QUOTE]
JavaScript itself isn't compatable with all browsers. More people use MSIE than use JavaScript, so we know that it doesn't even work for all MSIE users. And when you start playing around with non-standard extensions to JavaScript, as you are trying to do, the level of support drops even further.
×

Success!

Help @nofx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...