Check if file is equal to

JavaScript related questions should go here.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Check if file is equal to

Post by FrederickGeek8 »

How do I do something so if would be like
[syntax=php]<?php
$contents = file_get_contents('controller.txt');
if($contents == 'hey'){
echo 'it is hey';
}elseif($contents === 'no'){
echo 'it is no';
}
?>[/syntax]

In javascript/jquery?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check if file is equal to

Post by jacek »

You can't. JavaScript has no access to the file on the server so it has no way to check if it exists. The closest you can get is to create a script to do the check for you and make an AJAX request to it.
Image
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Check if file is equal to

Post by FrederickGeek8 »

Even if the file is in the public domain? So what if it was like 'http://somesite.org/controller.txt'
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check if file is equal to

Post by jacek »

FrederickGeek8 wrote:Even if the file is in the public domain? So what if it was like 'http://somesite.org/controller.txt'

For a URL like that you can get the contents using an AJAX request (google it, many many example), this is only possible if the file is hosted on the same domain as the website.
Image
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Check if file is equal to

Post by FrederickGeek8 »

OK. So I was able to achieve this from

[syntax=javascript]var previous = "bad2";

setInterval(function() {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
if (ajax.responseText != previous) {
window.location.reload();
previous = ajax.responseText;
}
}
};
ajax.open("POST", "control", true); //Use POST to avoid caching
ajax.send();
}, 500);[/syntax]

the variable previous is generated by PHP when the page is reload. Now this all works perfectly fine if I am accessing this from its external ip, but if I am trying to access it from a domain name linked to that ip, it does not work.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Check if file is equal to

Post by jacek »

FrederickGeek8 wrote:but if I am trying to access it from a domain name linked to that ip, it does not work.

For security reasons pretty much every browser will not allow you to make a request to another domain like this, no way around it :(
Image
Post Reply