JavaScript - Reading From Text File Using Js
Dear frnd I wanna read on html page , and i have script :
" [I]<html> <head> <script type="text/javascript" language="javascript"> function Read() { var Scr = new ActiveXObject("Scripting.FileSystemObject"); var CTF = Scr .OpenTextFile("C:\\123.txt", 1, true); data = CTF .ReadAll(); document.write("<pre>" + data + "</pre>"); CTF .Close(); } </script> </head> <body onLoad="Read()" > </body> </html> [I] " But as its using activex control not allowing in all browser . Have you any other way with pure js to read the file contains ? or allowing activex in all browser ? Similar TutorialsHey guys, I'm writing a script and I've encountered a problem.. I have a txt file with many words, each word in a different line. For example: the file words.txt contains: word1 word2 word3 word4 word5 I need to load the content of the file into a variable in my script. I prefer that all the words will be in the same variable with line breaks, but if you'll figure out a way to put it in an array, it's ok too. I really don't know how to do it, and I tried to google but didn't understand.. By the way, I don't want to change the txt file to js file, I need it to remain txt.. Can anybody help me with that? Thanks Alot, Ran Hi all, How can i read CSV data from the text file using Java Script.Please give me the script for the problem. Thanx, Nari Somebody had posted this query on how to read text files using JavaScript.......can't find that post now... Anyways, though it's not possible to read text-files using JavaScript it is possible to read XML files using it. Here is an XML based JavaScript Ticker http://www.dynamicdrive.com/dynamicindex2/xmlticker.htm It works only on browsers supporting XML (IE) Enjoy! Hi I have written the following code to read contents of a text file using FileReader object of HTML 5 for Google Chrome. Quote: <script> function handle_files(files) { var i; if (checkBrowser("Chrome")) { for (i = 0; i < files.length; i++) { file = files[i]; var reader = new FileReader(); ret = []; reader.onload = function (e) { console.log(e.target.result) } reader.onerror = function (stuff) { console.log("error", stuff) console.log(stuff.getMessage()) } text = reader.readAsText(file[i]); alert(text); } } } </script> ---------------------- <input type="file" multiple="multiple" onchange="handle_files(this.files)"> Unfortunately, the variable text always displays as undefined. Everything above the line text = reader.readAsText(file[0]); works fine. How can I fix it?. Or is there any other solution to read a text file using HTML5/JavaScript on Chrome? Thanks in advance. i am thinking of using a xml file as a data base for example , the xml file stores a list of reference numbers which is allocated to a url or a name such as 0123456789 and i have a text box on a webpage and the user types in a reference number and the javascripts reads the xml file to check if its a valid reference number i.e a registered number i am new to working with xml etc... so would be nice to have a little help here EXAMPLE <DATA> <REF> <0123456789>JOE</01234567890> <1111111111>www.google.com</1111111111> </REF> </DATA> id like it to read the specific data thats contained within the tag such as 0123456789's tag = JOE Hi I'm reading a text file, manipulating the string that I've read, and then writing to a new text file called temp. The error that I am getting is basically reading past the end of the file. function SpaceFileEntries() { fso = new ActiveXObject("Scripting.FileSystemObject"); filename = "NDELPROF.txt"; path1 = document.getElementById("lblDataOutputDirectory").innerText + "\\" + filename; path2 = document.getElementById("lblDataOutputDirectory").innerText + "\\temp.txt"; s = fso.OpenTextFile(path1); j = fso.CreateTextFile(path2 , true); do { text = s.readLine(); partOne = text.substring(0,34); lastPart = text.substring(35, 54); space = " "; j.write(partOne); j.write(space); j.writeline(lastPart); } while ( text != null ) s.Close(); j.Close(); } Can someone please help me find this bug in the code. I know it's got to do with the while statement, but can't think or find good alternatives. Thanks. Daniel Hi I have a large'ish project under development. The code below distills the problem I am having. In essence, I am trying to read in an XML file. It appears to load okay but when I try to count how many there are of a certain tag (The "Team" tag) I am told that there are zero. This is despite being told that that file loads okay (as the callback function is called). Javascript and the xml file is below. I hope that it is easy to follow. When the page is loaded, the initialize function is called. I first check my browser as I only need this to work on a Mozilla browser (I am using Mozilla Firefox 3.6.3). I then load the xml file and the callback function (fileLoaded) is called. I then try to find out how many "Team" tags I have, but get the message that there are zero. Sorry if this is a simple question but I have spent the best part of yesterday trying to get this to work. Any help would be appreciated. Thx G === javascript === Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <script type="text/javascript"> // Global variable so that everything can access it var xlmDoc; function initialize() { // This function gets called when the page loadds browserCheck(); xmlDoc=document.implementation.createDocument("", "doc", null) xmlDoc.load("small.xml"); // This file is shown at the end of this document, but it is a separate xml document xmlDoc.onload = fileLoaded; } // End of initialize function fileLoaded() { // Called when file is loaded (this does get called as expected) alert("File loaded"); var teams = xmlDoc.getElementsByTagName("Team"); var count = teams.length; alert("no of Teams: " + count); // This displays zero - WHY??? } // End of fileLoaded function browserCheck() { // START: This function just checks that I am using a Mozilla browser (I am) var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); if(moz) { alert("Mozilla"); } else {alert("Not Mozilla"); } } // End of browserCheck </script> </head> <body onload="initialize()"> </body> </html> === xml file === Code: <Root> <Team> <Type>GLF</Type> <Name>Verulam</Name> <Postcode>AL1 1JG</Postcode> <Page>693</Page> </Team> <Team> <Type>RFU</Type> <Name>London Manx RFC</Name> <Postcode>AL1 2DJ</Postcode> <Page>704</Page> </Team> <Team> <Type>RFU</Type> <Name>University of Hertfordshire RFC</Name> <Postcode>AL10 9AB</Postcode> <Page>729</Page> </Team> </Root> I would like to ask is it posible to add text from a text file to a text area on a page.. Philp if you read this i am not asking for a javascript code i am asking wether or not it can be done. P.s if your a nice person like philp is not and you want to private message me if you have one 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") >= 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) Is allowing a button a create a text file a security issue? If so, how do I collect the names of users who have clicked a certain button?
Hello, I am building a web application that has quotes on it. I am planning on having all the quotes stored in a .txt file. A line space would separate the quotes. Example: "Blah Blah" "Idk what blah" "sure you do" Would this be using Javascript to pull the quotes from the txt file for use in my page be best? I need it to pull all the quotes. Reason for having it like this, I am also planning on having a random quote page where it just pulls 1 quote instead of showing all of them. I'm also open to other ideas here! David Faircloth Is there any possibility to write a text file using Chrome/FF? On the other words, writing a text file w/o using ActiveX. Thank you.
I'm pretty much lost at javascript, and unfortunately can use php to accomplish this, so here goes........ I have existing html page that includes a Week #. ie, 1, 2, 3, 5.... and so on. I'd like to be able to have a text, csv, or similar file that I can update on frequent basis that will list a Week # and than custom field of some type. (likely a date or unix time stamp). Is there a way to open a text file (or csv, etc) in javascript. Than be able to compare and get the correct data? Off the top of my head, I think its possible, and would involve something like the following? 1. Create a csv file like below 1, this is custom, my other custom 2, some more, and this too 3, and somthing, see spot run 2. Next would be have a javascript open and read the file. You would assign a variable name to each 1st, 2nd and 3rd item of each line. Than the script would loop through each line until it found a a match of Variable 1 to the Week your looking for. You could then parse out and use the other variables from that line. Any of that make sense? I hope to try and experiment some later today or tonight if I can, but javascript is still pretty foreign to me. hi, i have looked all over the internet but i can't find anything that works. All i want to do is simply read from a text file. Thanks I'm new to Javascript and having some issues, so I'll aplogize right off for what I know is likely a very simple problem. I have a script where I'm 1) reading in a text file 2)remove multiple spaces between values in the file, 3) load these values into an array 4) format a table from this array. The script returns the following error which I can't seem to resolve: (12, 8) Microsoft JScript runtime error: Object doesn't support this property or method Code: var ForReading = 1 strHostname="XX.XX.XX.XX"; objFSO = new ActiveXObject("Scripting.FileSystemObject") objTextFile = objFSO.OpenTextFile ("E:\Integrity.txt", ForReading) strDelTab = "<table border='1'><tr><td style='background-color:#c5d1db; text-align:center'>Server</td><td style='background-color:#c5d1db; text-align:center'>User</td><td style='background-color:#c5d1db; text-align:center'>Group</td><td style='background-color:#c5d1db; text-align:center'>User</td></tr>"; while (!objTextFile.eof) { strDeletes = objTextFile.readln(); strDeletes = Replace(strDeletes, " "/g, " "); arrDeletes = Split(strDeletes, " "); strDelTab += "<tr><td>" + arrDeletes[0] + "</td>" ; strDelTab += "<td>" + arrDeletes[1] + "</td>"; strDelTab += "<td>" + arrDeletes[2] + "</td></tr>"; } strDelTab += "</table>"; objTextFile.close(); Wscript.Echo (strDelTab); Any assistance would be appreciated. Thanks Dear friend , I am uploading text file on html page , that text file lies on desktop and getting updated at every hour . I will keep my html page on server so how will it upload that text file from my desktop ? it is being generated by one cloud sensor . Hi, I have an idea for putting news articles on my phone to read later when I have time. I'd like to be able to right-click the text of an article and choose "Send to Phone Queue." When I've got a few articles in the queue, I'd like to be able to bluetooth the file to my phone. I know some HMTL and CSS, but I'm very much a novice when it comes to JavaScript. I'm thinking that I probably can't even do a lot of this with JS because of it's limitations (i.e. writing to a file). Will I need to use PHP or Java for writing/bluetoothing the file? I was playing around with extracting the body-text of an article from FoxNews.com. I didn't get very far but you can see where I'm going with it. I welcome any comments, criticisms, etc. Thanks for anything you say, even shooting this down as unrealistic. Code: function getBodyText() { var divList = document.getElementsByTagName('div'); for ( i=0; i<=divList.length; i++) { if (divList[i].getAttribute("class") == "entry-content KonaBody") { . . . Hi, i want to open a file in a text area, such that the user can either browse for a file and when selected it will open in a text box on the page, or the user can select a file from a list of active links on the page and this will open in the text area. I am new to java and dont know where to start on this, i have been browsing resources on the internet but have not found anything to get me started, would anybody be able to help? Thanks Im trying to use javascript to search a text file. text file is like this... 1111111[TAB]Project Name[TAB]Application1 2222222[TAB]Project Name[TAB]Application2 Im trying to get the javascript to let a user search for number or project name, if found it displays all the info if found. application1 or application2 (whatever is in the 3rd area) The text file can also be seperated by commas, either way is fine. can anyone help with this. ? Hello, i m new Javascript but knows basics of functions etc. ive created a website, which also has field to Submit Email addresses for newsletters.. Now, i have a TextBox (for users to write email addresses) and a Submit button in which i want to call a Javascipt script function which OnClick takes the value(email address) from the textbox and saves it to a .txt file on server. i think i am just missing a single line, the function name which will store the value.. i searched ovr the net but didnt find it.. plz help mee. Thankyou! |