Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First I'll assume that you ultimately want to <em>read a local file</em> with your browser and your current workflow is something like a local 'bash-script' that </p> <ol> <li>first updates/modifies an inline piece of javascript (inside a locally stored html file) with the last occurring value retrieved from a local txt-file (using sed)</li> <li>opens the (just modified html-) file (via commandline) inside a common browser.</li> </ol> <p>Then I assume the sed-route once worked but now doesn't work anymore (probably because the html file has changed?) and now you'd like the inline javascript (in the html file) to fetch that value from the textfile itself and subsequently use it (thus without the need for the 'bash-script'/sed solution.</p> <p>Thus, the answer (based on above assumptions) to your final question: '<em>is there any javascript code that takes that file, extracts the last value and simply displays it on the right place of the code?</em>', depends on your final requirement:<br> are you ok with a file-input where you select the text-file <em>every</em> time you view the html-file? </p> <p>If your answer is YES, then, (depending on the browser you use) you <strong>can</strong> read a local file (and work your magic on it's contents).<br> In modern browsers, using the <a href="https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications" rel="nofollow noreferrer">File API</a> (which was added to the DOM in HTML5) it's now possible for web content to ask the user to select local files, then read the contents of those files.</p> <p>For example, using FireFox's '<a href="https://developer.mozilla.org/en-US/docs/Web/API/FileReader" rel="nofollow noreferrer">FileReader</a>' you could do:</p> <p>html:</p> <pre><code>&lt;input type="file" id="fileinput" multiple /&gt; </code></pre> <p>javascript:</p> <pre><code>function readAllFiles(evt){ var files = evt.target.files, i = 0, r, f; if(files){ for(; f = files[i++]; ){ r = new FileReader(); r.onload = (function(f){ return function(e){ alert(e.target.result); }; })(f); r.readAsText(f); } } else { alert("Error loading files"); } } document.getElementById('fileinput') .addEventListener('change', readAllFiles, false); </code></pre> <p><em>Note that for accessing local files in Chrome you must start Chrome with this switch: <code>chrome --disable-web-security</code></em> </p> <p>However,<br> if the answer is NO (so you want to specify the file, and more importantly it's path, inside the 'code', so you don't have to select the text-file every time your local app runs) then you (usually) <strong>can't</strong> (because you <a href="https://stackoverflow.com/a/15201258/588079">can't get/set the path</a>, thank the great maker)...<br> Unless you choose a specific older/unpatched browser (specifically for this task) where you know of a (hack) way to do this anyway (like the IE <a href="http://seclists.org/bugtraq/2002/Aug/258" rel="nofollow noreferrer">xml vulnerability</a> or the <a href="http://technet.microsoft.com/en-us/security/bulletin/ms02-008" rel="nofollow noreferrer">XMLHTTP vulnerability</a> or etc... you get the picture..).</p> <p>Some alternative solutions (that don't require you to select the correct textfile over and over again)</p> <ol> <li>Setup a fullblown web (LAMP) server (to use the XMLHttpRequest way as used in aross answer, but this might feel like shooting at a mosquito with a cannon..) </li> <li>Explore different script languages (but effectively still do the same as your now broken sed-solution)</li> <li>Combine 1 and 2, choosing from php (the latest version has a small webserver included, you might start/stop it when needed (even in the bash-script workflow) OR using node.js (which is 'javascript' <em>and</em> where you can program/control a small task-specific server in just a couple of lines).</li> </ol> <p>Hope this helps!</p> <p><hr> <strong>Update:</strong><br> Based on your updated question, comments and request for recommendation, I'd like to suggest to use PHP to dynamically fetch the value from your log txt file and have it generate your html code with inline javascript on the fly (every time you visit the page). </p> <p>The browser will never see the php code, only what php inserted to your page (in this example the last found value or 0).</p> <p>You'd rename the <code>rightnow.html</code> file to <code>rightnow.php</code> and modify it (something like) this:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt;&lt;head&gt; &lt;!-- your header code --&gt; &lt;script type="text/javascript"&gt; //parts of your javascript &lt;?php // start php script $logFile= '/pathToYour/logFile.log'; // &lt;-Modify if( $fp= @fopen($logFile, "r") ){ // if logfile succesfully opened, fseek($fp, -30, SEEK_END); // set pointer 30 chars from EOF $val= array_pop(explode("-", rtrim(fread($fp, 30)))); //get last value fclose($fp); // close file } ?&gt; // end php script function drawChart(){ var data=google.visualization .arrayToDataTable([ ['Label', 'Value'], ['Temp', &lt;?php echo $val? $val : "0"; ?&gt;], ]); // ^php above inserts value or 0 // etc... } //parts of your javascript &lt;/script&gt; &lt;/head&gt;&lt;body&gt; &lt;!-- your body code --&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <p>Note that <a href="http://ca.php.net/manual/en/function.fopen.php" rel="nofollow noreferrer"><code>fopen</code></a> in combination with setting the filepointer via <a href="http://us.php.net/manual/en/function.fseek.php" rel="nofollow noreferrer"><code>fseek</code></a> and sequentially <a href="http://nl3.php.net/manual/en/function.fread.php" rel="nofollow noreferrer"><code>fread</code></a>-ing from the pointer to EOF <em>does not load the complete logfile (60min * 24hour=1440 lines of 16 bytes=22.5kB at the end of the day) into memory</em> (<em>good</em> for this purpose), but only the last 30 chars (as in this example).</p> <p>The variable to your logfile and path must still be modified to your situation (I don't know the format of your <code>$today</code> variable). </p> <p>Depending on your further needs you might want to perform some extra checks/logic on the array of values that explode returns (instead of popping the last value). Or what about modifying the html a little so you could also include the last temperature's time reading, etc. (But this tested piece of code should get you started and explains the procedure of going the php way).</p> <p><hr> <strong>Update:</strong><br> Since you have chosen to place the last known value of your logfile as in textfile placed <em>inside</em> your public www-root (with a bash script I assume, every minute of the day?), you can now indeed go the 'ajax' way, as answered by aross! </p> <p>However I want to hint that the code/solutions in all current answers here could be mixed (since you now also have ajax working): instead of ajax-ing (loading) a txt file, you could have php fetch and send this value to the browser on-the-fly/on-demand! </p> <p>So, instead of requesting <code>http://url_to_my_rpi/file_to_download.txt</code>, you could request <code>http://url_to_my_rpi/read_last_temperature.PHP</code> which should fetch the last known value out of the log-file (set proper security/access) and send it to the browser (set proper headers), just like your text-file did. You wouldn't have to change anything in the html/javascript except the url you request.</p> <p>The advantage would be (depending on how your current bash-scripts works) that your PI now only does this 'work' (of getting the last value of your logfile) when you are viewing your monitor-page. And that you are not writing that file in your www-root every minute of every day (as I suspect).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload