Javascript: Open a file, store lines in array, if certain text exists...
4 replies, posted
I'm having major pains trying to figure this out. I'm kind of new to Javascript, I need to open a text file from an external server, store each line in an array, then search that array for a certain word (HIGH), and if it exists then write something to the webpage, and if not, write something else. Here is what I have so far:
[code]
<html>
<head>
<title>Test</title>
<script>
<!--
function test(x) {
if (wxd1txt.readyState === 4 && wxd1txt.status === 200) { // Makes sure the document is ready to parse and Makes sure it's found the file.
var wxd1text = wxd1txt.responseText;
var wxd1array = wxd1txt.responseText.split("\n"); // Will separate each line into an array
var wxd1high = wxd1array.toString(); //Converting the String content to String
//var highsearchreg = new RegExp("HIGH");
//var wxd1high = wxd1array[x].search(highsearchreg);
document.write(wxd1high);
if (wxd1high.search("HIGH RISK") >= 0){
document.write("HIGH RISK");}
else {
document.write("NO RISK");}
}
}
//-->
</script>
</head>
<body>
Hi!
<script>
<!--
var Today = new Date();
var ThisDay = Today.getDate();
var ThisMonth = Today.getMonth()+1;
var ThisYear = Today.getYear();
var Hour = Today.getHours();
var Day2 = Today.getDate()+1;
var Day3 = Today.getDate()+2;
if (navigator.appName != "Microsoft Internet Explorer") {
ThisYear = ThisYear + 1900;}
if (ThisMonth < 10) {
ThisMonth = "0" + ThisMonth;}
if (ThisDay < 10) {
ThisDay = "0" + ThisDay;}
if (Hour == 2 || Hour == 22 || Hour == 23 || Hour == 0 || Hour == 1) {
var wxHourd1 = 0600}
else if (Hour >= 3 && Hour <= 10) {
var wxHourd1 = 1300;}
else if (Hour >= 11 && Hour <= 13) {
var wxHourd1 = 1630;}
else if (Hour >= 14 && Hour <= 16) {
var wxHourd1 = 2000;}
else if (Hour >= 17 && Hour <= 21) {
var wxHourd1 = 0100;}
//var wxurld1 = "http://www.spc.noaa.gov/products/outlook/archive/"+ThisYear+"/KWNSPTSDY1_"+ThisYear+""+ThisMonth+""+ThisDay+""+wxHourd1+".txt";
var wxurld1 = "http://www.spc.noaa.gov/products/outlook/archive/2010/KWNSPTSDY1_201005101300.txt" //(High risk day for testing)
//document.write(wxurld1); //Use this to verify this section is working
if (window.XMLHttpRequest)
{
wxd1txt=new XMLHttpRequest();
}
else // IE 5/6
{
wxd1txt=new ActiveXObject("Microsoft.XMLHTTP");
}
wxd1txt.open("GET", wxurld1, true);
wxd1txt.onreadystatechange = test();
// -->
</script>
</body>
</html>[/code]
When added to a webpage, nothing shows up except the "Hi!" and there are no errors in the Javascript Console in Google Chrome. Is this possible with Javascript, and if so, what am I doing wrong or not doing?
Also, I have 2 URLs, one is a text file that has the HIGH text I want for an example, the other is the current file, which shouldn't have HIGH in it (unless the weather in the US turns really bad)
You can't use XMLHttpRequest cross-domain
document.write overwrites the whole page if the DOM has already finished loading
move the first script block to underneath the second, you're using undefined variables
[QUOTE=turb_;21951800]move the first script block to underneath the second, you're using undefined variables[/QUOTE]
Moved it down, still the same. Only shows "Hi!" and still no errors in console
Commented out the document.write sections that weren't part of the if statements and it doesn't work
Sorry, you need to Log In to post a reply to this thread.