I'm trying to write code that will take Davis's forecastRule (a number) and display this on my webpage as text by reading the number off an array and returning the text corresponding to that number.--
For example if the Davis forecastRule returned is 5, I want the webpage to display "Partly cloudy and cooler" which is the corresponding forecast in text form.
I'm not sure on the best way to do this but am trying to do it with Javascript with instructions to pull the text from an external text file (set up as a JS array) containing the nearly 200 text strings.
I'm getting bogged down with the syntax and would appreciate any help. Should I be doing it a different way ie not using JS?I don't know how to declare the variable if the array lives in an external file.Thanks for any help!PJD
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/f562b9c5-c551-45b2-a9a4-25848d847aad%40googlegroups.com.
// forecast descriptions associated with forecastRule.// one forecast desc per line, the first line corresponds to forecastRule 0var FORECASTRULES_FILE = "forecastrules.txt”;… …var Forecast_list = null; // table of forecast stringsfunction start_gauges_updating() {// get and remember the forecast descriptionsif (! Forecast_list) {var text = loadSmallTextFile(FORECASTRULES_FILE);if (text) {Forecast_list = text.split('\n');} else {console.error(FORECASTRULES_FILE + ": no forecasts provided");Forecast_list = [];}}… …// remember the forecast element, updated for each forecast update.// usually populated by a description from forecast_list, major errors// are also flagged hereforecast_el = document.getElementById("gauges_forecast"); // NOT var… …}… …// update forecastvar forecast;var fcrule = observ_list['forecast’]; // <<<< however you get your fcrule valueif (fcrule) {forecast = Forecast_list[fcrule];}if (forecast) {forecast_el.textContent = forecast;} else {forecast_el.textContent = "Forecast not available";}… …// obtain contents of a small file.// uses synchronous GET so file should be quite small//function loadSmallTextFile(file_name) {var xhr = new XMLHttpRequest();xhr.overrideMimeType('application/json');xhr.onerror = function() {console.error(“loadSmallTextFile: " + xhr.statusText);};var d = new Date();var fnam = file_name + "?v=" + d.getTime(); // defeat cachexhr.open('GET', fnam, false); // synchronousxhr.send(null);var file_text; // defaults to undefinedif (xhr.readyState == 4 && (xhr.status == 0 || xhr.status == 200)) {file_text = xhr.responseText;}return file_text;}