JavaScript - Iterating Through Lines In An Array
I am new to javascript but am proficient in .net languages.
If I have a text file that is structured as follows; -35.889,150.984 -35.578,151.022 -35.449,150.993 I have two questions; 1. How can I read the text in that file into an array? I've tried Code: var file = "X:\ASP.Net\GoogleMaps\GMAP\MarkerCoordinates.txt"; //file:\\X:\ASP.Net\GoogleMaps\GMAP\MarkerCoordinates.txt $.get(file,function(txt) P.S. I tried the commented way above also and that didnt work I also tried using XMLHttprequest like so Code: var txtFile = new XMLHttpRequest(); txtFile.open("GET", "file:\\C:Gmap\test.txt", true); 2. How do you iterate through each line in the array? I'm not sure how to do a for-each in javascript, would be simple in .net Thank you, in advance, for any assistance Similar TutorialsI'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") >= 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> 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) hi, Does any one knows the javascript that read step by step single line from form and then store in array , and that array use 4 further references Please help i need this Please Please Please Thanx Regards Fawad Hi everybody, I have a php file with an almost-standard html form. The not-so-standard part is that the name-attributes of the form elements are dynamically generated as such: PHP Code: <? for($i=0; $i<5; $i++) { ?> <input type="text" name="field_<? echo $i; ?>" /> <? } ?> Now I want to do some javascript validation on the fields, but I'm having a bit of trouble accessing the values of the fields. My current code is as follows: Code: var i = 0; var num = <? echo $step1_sag_faktura_num; ?>; for (i=0; i<num; i++){ window['file_' + i] = document.forms["nyisagform_step2"]["sag_fak_file_" + i].value; } But this doesn't seem to work. It gives no error message. It just validates true. Can somebody tell me a way to do this? I'm no good at javascript and the code above is pieced together by bit I found on the internet. Thanks in advance, deafdigit Hello every body i have a problem with javascript i wrote a script which process the entry of a text area 1 and i made the script process only the first line of the entry i want a method to make the script to process all of the lines which the user enter it OR make a varibals to every line entered from the user i am so sorry about the weak language thank you in advance i have a program which gets values from different arrays and displays them on screen. how do i insert a break every 5 lines so its easier to read?
Here I have a code to redirect a page to one of these three pages. But is there a limit to the amount of websites I can add? Code: <script type="text/javascript"> var urls = new Array(); urls[0] = "http://www.google.com"; urls[1] = "http://www.youtube.com"; urls[2] = "http://www.codingforums.com"; var random = Math.floor(Math.random()*urls.length); window.location = urls[random]; </script> Would I be allowed to have, say, hundreds of thousands of different sites? Or would the script stop working? Thanks. I'm making a graph in Canvas. I plot the grid with ctx.strokeStyle = '#ccc'; I then plot the data lines with ctx.strokeStyle = '#000';, and the GRID color then turns back to '#000' too! The same happens with any color. The same happens with lineWidth. The fillStyle of the labels is not affected. It happens even if I surround the code with save/restore. It happens in both Firefox and Chrome. Has anyone else had this problem? Is there any way around it? Has anyone ever tried to break up a text area into 2 lines, but at the same time each line has a limited number of characters allowed in it ? i.e. line 1 = maxcharacters = 124 , line 2 = machcharacters = 1024 (Line 2 will therefore wrap until 1024 chracters are reached) ... including spaces as characters. Something along the lines of: Code: var maxLines = txtArea.rows; var maxChars = txtArea.rows * txtArea.cols; countChars.value = txtArea.value.length; countLines.value = getLines(txtArea); document.myForm.maxLines.value = maxLines; document.myForm.maxChars.value = maxChars; if((txtArea.value.length >= maxChars || getLines(txtArea) >= maxLines) && (window.event.keyCode == 10 || window.event.keyCode == 13)) { while(getLines(txtArea) > maxLines) txtArea.value = txtArea.value.substr(0,txtArea.value.length-2); while(txtArea.value.length > maxChars) txtArea.value = txtArea.value.substr(0,txtArea.value.length-2); alert("chars and / or lines limit reached"); } else if(txtArea.value.length > maxChars ) { while(txtArea.value.length > maxChars) { txtArea.value = txtArea.value.substr(0,txtArea.value.length-1); } alert("chars limit reached"); } else if (f (countLines.value > maxLines) { while(countLines.value > maxLines) { txtArea.value = txtArea.value.substr(0,txtArea.value.length-1); } alert("lines limit reached"); } countChars.value = txtArea.value.length; countLines.value = getLines(txtArea); } Thanks Hi. I have the following line in my HTML file <table class="bz_buglist" cellspacing="0" cellpadding="4" width="100%"> And I need to use javascript to change this line to <table id="main_table" class="bz_buglist" cellspacing="0" cellpadding="4" width="100%"> . I just need to add the ID part. How can I do this? Is there a method of replacing the line, or a way to "add attribute"? thanks! kev. Hi - I have the following requirement for a textarea: Limit the number of lines to 8 Limit the number of characters to 20 per line Force a line beak when the user reaches he end of a line but break the line at the start of the last word (not half way through the word). Allow pasting into the field and auto-format the line/character count Keep he cursor where it should be (don't move it to the end of the field after any formatting) This is more complicated than it sounds - anyone worked on something similar? This is my first posting to this forum. I have list of about a thousand applications for which various outsourcing companies are supporting at different levels. I want to use the attached code to filter. For example, if I type "SOX", every row with "SOX" appears. The filter works well but when I type "S" and the information appears on row, the height of the row changes. When I type "O" it changes more. What must I change to keep the height of the rows consistent. Thank you <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Filter applications</title> </head> <body> <table style="width: 646px; height: 926px;" id="content"> <tbody> <tr> <td id="main"> <div class="node"> <div class="content"><img style="width: 636px; height: 183px;" alt="" src="../Common%20Graphics/header.png" /><br /> <big><span style="font-family: Arial;">Governing outsourcing companies servicing applications</span></big><br /> <br /> <script type="text/javascript"> function filter (phrase, _id){ var words = phrase.value.toLowerCase().split(" "); var table = document.getElementById(_id); var ele; for (var r = 1; r < table.rows.length; r++){ ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,""); var displayStyle = 'none'; for (var i = 0; i < words.length; i++) { if (ele.toLowerCase().indexOf(words[i])>=0) displayStyle = ''; else { displayStyle = 'none'; break; } } table.rows[r].style.display = displayStyle; } } </script> <form><b style="font-family: Arial;">Filter:</b> <input name="filt" onkeyup="filter(this, 'sf')" type="text" /></form> <br /> <hr style="width: 100%; height: 2px;" /> <table id="sf" style="width: 636px; height: 994px;" border="0" cellpadding="2" cellspacing="1"> <tbody> <tr> <td style="font-family: Arial; background-color: rgb(255, 255, 153);"><font color="">Application</font></td> <td style="font-family: Arial; background-color: rgb(255, 255, 153);"><font color="">Outsourcing company</font></td> <td style="font-family: Arial; background-color: rgb(255, 255, 153);"><font color="">Support</font></td> </tr> <tr> <td style="font-family: Arial;"><a href="HTML/index.html" target="_blank">432 Reports Prod</a></td> <td style="font-family: Arial;">InfoSys </td> <td style="font-family: Arial;">Gold</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">Accomodation Reservation System</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Silver</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial; height: 30px;">ACL - Audit Command Language</td> <td style="width: 223px; font-family: Arial;">Accenture </td> <td style="width: 133px; font-family: Arial;">Gold SOX</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">Actions Tracker</td> <td style="width: 223px; font-family: Arial;">Tata </td> <td style="width: 133px; font-family: Arial;">Gold SOX Critical</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">Air Quality Public Website and Dispersion Forecast</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Silver SOX</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">Airport Management System</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Bronze</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">All Mines Grade Control</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Gold</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">ALLDATA</td> <td style="width: 223px; font-family: Arial;">Tata</td> <td style="width: 133px; font-family: Arial;">Gold Critical</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">AppAdmin</td> <td style="width: 223px; font-family: Arial;">Accenture</td> <td style="width: 133px; font-family: Arial;">Gold SOX</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">APPSRS</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Silver SOX</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">ArcGIS</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Silver</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">ASGLIB</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Gold Critical</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">Asset Integrity</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Bronze</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">Asset Integrity - FLPS QA Survey</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Silver</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">Asset Management Framework (FLPS)</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Gold</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">Auto Cad (MB)</td> <td style="width: 223px; font-family: Arial;">InfoSys</td> <td style="width: 133px; font-family: Arial;">Gold SOX</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">Autocad</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Gold SOX Critical</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">AutoMod</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Silver</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">AutoSkills</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Bronze</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">B Cleaner Circuit Control Summary (BCLDIS)</td> <td style="width: 223px; font-family: Arial;">InfoSys </td> <td style="width: 133px; font-family: Arial;">Silver SOX</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">BACCESS (Acton)</td> <td style="width: 223px; font-family: Arial;">Tata </td> <td style="width: 133px; font-family: Arial;">Bronze</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">BACSESS-IP</td> <td style="width: 223px; font-family: Arial;">Tata</td> <td style="width: 133px; font-family: Arial;">Gold</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">Bank deposit</td> <td style="width: 223px; font-family: Arial;">Accenture </td> <td style="width: 133px; font-family: Arial;">Gold SOX Critical</td> </tr> <tr bgcolor="#eaecef"> <td style="color: black; width: 256px; font-family: Arial;">Barometer Display</td> <td style="width: 223px; font-family: Arial;">Accenture </td> <td style="width: 133px; font-family: Arial;">Gold Critical</td> </tr> <tr bgcolor="#ffffff"> <td style="color: black; width: 256px; font-family: Arial;">Batch & Print Pro</td> <td style="width: 223px; font-family: Arial;">Tata </td> <td style="width: 133px; font-family: Arial;">Silver</td> </tr> </tbody> </table> </div> <br /> </div> </td> </tr> </tbody> </table> </body> </html> wait... why am i here???
I need to pass two lines of code through an onClick event.... player1.sendEvent('STOP'); player2.sendEvent('STOP'); I tried Code: <a href="javascript:void(0)" onClick="player1.sendEvent('STOP'); player2.SendEvent('STOP');" title="<?php echo $list[$i]->title ?>"> only the event of player1.sendEvent('STOP'); is working... but not the second one. How do I manage this right here on the link rather than calling a fuction. Thanks Hi guys. I'm using a jquery plugin called innerfade, which I got from he http://medienfreunde.com/lab/innerfade/ . I've used it to fade few lines of text into each other: Code: <div class="fade"> <p>Line 1 asdfghjkl</p> <p>Line 2 sdasdsafasdgedgr</p> <p>Line 3 ddddddddddddddddddddd</p> </div> So only first line appears on page load, then after few seconds fades into Line 2 etc. Everything works perfect, but there is only one problem - the text is not centered even if it says so in CSS. Since the lines have different lenghts, the shorter the line the more it's aligned to the left. I've added properties for both div and p's - tried all combinations of text-align: center and margin left & right: auto. No joy. But when I remove the script all 3 lines appear centered perfectly. So my question is how do I make them centered? I presume it has to be added in the JS part since it seems to be overriding the CSS. Here is the script's full code: Code: (function($) { $.fn.innerfade = function(options) { return this.each(function() { $.innerfade(this, options); }); }; $.innerfade = function(container, options) { var settings = { 'animationtype': 'fade', 'speed': 'normal', 'type': 'sequence', 'timeout': 2000, 'containerheight': 'auto', 'runningclass': 'innerfade', 'children': null }; if (options) $.extend(settings, options); if (settings.children === null) var elements = $(container).children(); else var elements = $(container).children(settings.children); if (elements.length > 1) { $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass); for (var i = 0; i < elements.length; i++) { $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide(); }; if (settings.type == "sequence") { setTimeout(function() { $.innerfade.next(elements, settings, 1, 0); }, settings.timeout); $(elements[0]).show(); } else if (settings.type == "random") { var last = Math.floor ( Math.random () * ( elements.length ) ); setTimeout(function() { do { current = Math.floor ( Math.random ( ) * ( elements.length ) ); } while (last == current ); $.innerfade.next(elements, settings, current, last); }, settings.timeout); $(elements[last]).show(); } else if ( settings.type == 'random_start' ) { settings.type = 'sequence'; var current = Math.floor ( Math.random () * ( elements.length ) ); setTimeout(function(){ $.innerfade.next(elements, settings, (current + 1) % elements.length, current); }, settings.timeout); $(elements[current]).show(); } else { alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\''); } } }; $.innerfade.next = function(elements, settings, current, last) { if (settings.animationtype == 'slide') { $(elements[last]).slideUp(settings.speed); $(elements[current]).slideDown(settings.speed); } else if (settings.animationtype == 'fade') { $(elements[last]).fadeOut(settings.speed); $(elements[current]).fadeIn(settings.speed, function() { removeFilter($(this)[0]); }); } else alert('Innerfade-animationtype must either be \'slide\' or \'fade\''); if (settings.type == "sequence") { if ((current + 1) < elements.length) { current = current + 1; last = current - 1; } else { current = 0; last = elements.length - 1; } } else if (settings.type == "random") { last = current; while (current == last) current = Math.floor(Math.random() * elements.length); } else alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\''); setTimeout((function() { $.innerfade.next(elements, settings, current, last); }), settings.timeout); }; })(jQuery); // **** remove Opacity-Filter in ie **** function removeFilter(element) { if(element.style.removeAttribute){ element.style.removeAttribute('filter'); } } And call: Code: $(document).ready( function(){ $('.fade').innerfade({ animationtype: 'fade', speed: 750, timeout: 5000, type: 'random', containerheight: '1em' }); }); Say I have a <textarea> input and I want to strip all the blank lines (lines with only whitespace). I have this regular expression, why doesn't it do anything? I've tried different variations with the flags (m and g). Code: ... var output = input.replace(/^\s*$/gm, "notworking!"); ... The very same regex works fine in my text editor (Notepad2), and I can match all empty lines. Code: LINE1 LINE4 ^(line 3 has some spaces) should become Code: LINE1 LINE4 And when I finally do get this to work, how would I remove the empty line? Hey all. I'm looking for a way to have a single, random line fetched from multiple text files, and then have that combination output somewhere, whether just to some field or another text file. For example: Text file A - I like I hate I love I despise I cherish Text file B - apples bananas oranges grapes grapefruit pineapple cherries Text file C - some of the time. most of the time. a lot of the time. pretty much never. So it would go in, get a line from each, and display something like: I love - grapefruit - most of the time. I cherish - pineapple - some of the time. etc. etc. I know it's a weird-sounding example, but you get the point. The line choices don't have to be separated by hyphens, but they preferably would be separated by something, like a comma, semi-colon, slash, whatever. I know this is pretty much a web coding forum, so that would be fine if I had to host it on my server or open it with a browser. But it would be optimal if this could be made into a standalone executable. I'm basically looking for something like Random Line Picker, just with the added functionality I mentioned. I'd be willing to make a little donation to someone if coding this would be a little difficult. Thanks! Is there a script that automatically applies two different styles to the first two lines of text in a container, without using CSS? Example: ====== This Would be Line One as a Title with its Own Style This would be line two as a byline with a separate style This is the main text of the paragraph... etc etc. ====== Thanks for any help. Steph Hi all, I hoping somebody can help me. I'm trying to acheive a line of text (with multiple links on each line) that change at set intervals. The code below seems to be working but I can only get working links on the first line that appears. Please can somebody help a gal out? xo [CODE] <html> <head> <title>Rotating Text</title> <script type="text/javascript"> var rotatingTextElement; var rotatingText = new Array(); var ctr = 0; function initRotateText() { rotatingTextElement = document.getElementById("textToChange"); rotatingText[0] = rotatingTextElement.innerHTML; rotatingText[1] = "why won't you work!?!?!?"; rotatingText[2] = "purdy please"; setInterval(rotateText, 5000); } function rotateText() { ctr++; if(ctr >= rotatingText.length) { ctr = 0; } rotatingTextElement.innerHTML = rotatingText[ctr]; } window.onload = initRotateText; </script> </head> <body> <span id="textToChange">log into your <a href="http://www.hotmail.com/">please</a> policy.</a> | can you <a href="http://www.google.com"> me?</a> </span> </body> </html> Hi, Does anyone know how to make a javascript regex ignore new lines? I have this Code: html = html.replace(/<!--(.*?)-->/gi, ''); // HTML comments which matches single line comments like - Code: <!-- this is a comment --> but not this - Code: <!-- This is a comment on multiple lines --> I think the answer lies with [\s\S] but I can't get it to work e.g. - Code: html = html.replace(/<!--(.*[\s\S]?)-->/gi, ''); // HTML comments Any help will be greatly appreciated Cheers Al |