JavaScript - Read Contents Of File And Act On Them
I use a PHP page to set elements of a text file on our web server and I want to create a javascript function to intermitently check the contents of that file and if an element in that file matches a criteria I want to act on it...
So, I do this <script type="text/javascript"> t = setInterval("CHECKFILE()",5000); </script> This calls the CHECKFILE function every 5000ms <script type="text/javascript"> function CHECKFILE(){ } </script> How, in the CHECKFILE function can I read from a file called FILE.txt for example?? In PHP I can use $myFile = "FILE.txt"; $theData = file($myFile); And this gives me an array, with each element containing one line from the file Can I do similar in Javascript? Or can I add PHP into the JS to do it for me? Thanks Similar TutorialsHi, i need to read a files contents to an array so that i can check which lines of the file i need to write back. im not very good with arrays and haven't had any luck so far. thanks I need to make a javascript that when I am at a page within a game I play and click the javascript, it will get the value of a table cell, separate them to x and y variables and then put the values into a link that opens. The open link is of a php script that uses $_GET for the values of the variables to query my database. I am terrible at javascript and have been trying to do this for 2 days now with no success. So I hope I can find help here. Here's the scripts so it is easier to understand: This is part of the html page that is from the game. I need to have the javascript get 432|608 and assign x=432 and y=608 in this case, although those number change, the place in the table that that they are displayed do not change. <td>Coordinates:</td> <td><a href="game.php?village=19392&s=map&x=432&y=608">432|608</a></td> <html> <head> <title></title> <meta http-equiv="Content-Language" content="de"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta http-equiv="Page-Enter" content="revealTrans(Duration=0.1,Transition=4)"> <link rel="stylesheet" type="text/css" href="kingsage.css"> <link rel="shortcut icon" href="favicon.ico" type="image/ico"> <script src="js/kingsage.js?hash=1e4c479f8ba66202f3b1bfb74414eef4" type="text/javascript"></script> <script src="js/mootools-1.2-core-nc.js?hash=1e4c479f8ba66202f3b1bfb74414eef4" type="text/javascript"></script> <script src="js/mootools-1.2-more.js?hash=1e4c479f8ba66202f3b1bfb74414eef4" type="text/javascript"></script> <script src="js/map_src.js?hash=1e4c479f8ba66202f3b1bfb74414eef4" type="text/javascript"></script> </head> <body> <script type="text/javascript"> //<![CDATA[ lang = new Array(); lang['DAY'] = 'Day'; lang['DAYS'] = 'Days'; //]]> </script> <div style="background: rgb(0, 0, 0) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;" align="center"> <table style="border-collapse: collapse; height: 100%;" width="100%" cellpadding="0" cellspacing="0"> <tbody> <tr valign="top"> <td width="50%" align="right"> <br> </td> <td style="width: 840px;"> <table style="border-collapse: collapse; width: 840px; height: 100%;" cellpadding="0" cellspacing="0"> <tbody> <tr> <td class="lay_content" valign="top"><a name="head2"></a> <div class="contentpane"> <table class="borderlist" style="width: 420px;"> <tbody> <tr> <th colspan="2">Abandoned settlement</th> </tr> <tr> <td>Coordinates:</td> <td><a href="game.php?village=19392&s=map&x=432&y=608">432|608</a></td> </tr> <tr> <td>Points:</td> <td>390</td> </tr> <tr> <td>Player:</td> <td><br> </td> </tr> <tr> <td>Alliance:</td> <td><a href="game.php?village=19392&s=info_ally&id="></a><br> </td> </tr> </tbody> </table> <br> </div> </td> </tr> </tbody> </table> </td> <td width="50%" align="left"> <br> </td> </tr> </tbody> </table> </div> <div id="settlement" style="display: none;"></div> <script type="text/javascript"> //<![CDATA[ startCounter(); //]]> </script> </body> </html> Here's the php script that will process the x and y Code: <?php putenv("TZ=Europe/Berlin"); session_start(); include("includes/config.php"); $username = $_GET['username']; $pass = $_GET['pass']; $x = $_GET['x']; $y = $_GET['y']; $now = time(); //confirm user can make claim global $conn; $q = "SELECT password FROM users WHERE username = '$username' AND active = 1"; $result = mysql_query($q,$conn); if(!$result || (mysql_numrows($result) < 1)) { $data = "Your identity can not be authenticated"; } else { $dbarray = mysql_fetch_array($result); if($pass == $dbarray['password']) { $continue = true; } else { $data = "Your password can not be authenticated"; } } //if can claim then check the village if($continue) { $q = "SELECT * FROM `claims` WHERE x = '$x' AND y = '$y' AND state = 1"; $village = mysql_query($q, $conn); $result = mysql_fetch_assoc($village); if(!$result) { $can_claim = true; } elseif($result) { if($result['username'] == $username) { $data = "You already have an active claim on ($x|$y)"; } elseif($result['username'] <> $username) { $claimer = $result['username']; $data = "$claimer already has an active claim on ($x|$y)"; } } if($can_claim == true) { global $conn; $claim_made = time(); $claim_expires = $claim_made + 432000; $state = 1; $q = "INSERT INTO `claims` (x, y, username, claim_made, claim_expires, state) VALUES ('$x', '$y', '$username', '$claim_made', '$claim_expires', '$state')"; $add_claim = mysql_query($q, $conn); if($add_claim) { $data = "Success! You claimed ($x|$y)"; } else { $data = "Error: Your claim on ($x|$y) did not complete."; } } echo "<SCRIPT>alert(\"$data\");</SCRIPT>"; } ?> The javascript should link to this php script and send the variables like this: 'http://twv-kingsage.tw-family.us/auto_add_claim.php?username=username&pass=md5_password&x="+x+"&y="+y' Can someone tell me how to extract the values for x and y from the page and get them to the link? Thanks for the help, it is much appreciated. I have a very simple page that has one iframe on it. What I would like to accomplish is having Javascript read the contents of the iframe and write it to the page. I have tried numerous coding samples found all over the web but nothing seems to work for me as I am a major Javascript noob. The iFrame will include content like the following which will be from an outside web server; Code: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><response><results><![CDATA[Success]]></results></response> Thanks a lot in advance! Hello, I have a DFXP formatted XML file (used for closed captioning), and I have tried several times unsuccessfully to load and show its contents on screen using js. Here is what the XML looks like Code: <tt xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xmlns="http://www.w3.org/2006/04/ttaf1" xml:lang=""> <head> <styling> <style tts:fontSize="14" tts:textAlign="center" tts:wrapOption="wrap" id="1"/> </styling> </head> <body> <div xml:lang="en"> <p begin="00:00:01.45" style="1" end="00:00:03.55"> PROFESSOR: Welcome to your <br/> virtual classroom. </p> </div> </body> </tt> I have the URL of the file and all I want to do is print the text onto the screen (the "PROFESSOR: Welcome to your virtual classroom." portion). If anyone can help me out I would greatly appreciate it, I'm stuck! 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 hi i m creating a webpage using javascript and php where i need to upload a doc file or zip and read all the words in doc file if it is zip i need to read all the words in all the doc files. i m trying to upload using file input but it does not show path of file only file name how i m suppose to upload and read it. Ok, I am currently working on an announcements portlet for a dashboard that my team is creating. What is desired is to be able to have this portlet access a stored text file (file can be edited so content is subject to change) that will have all the information needed for these announcements. The JavaScript will then write it into a html file. I am a novice at html, and javascript; and i was hoping that if anyone had any advice, ideas, or a helpful link that could point me in the right direction I would much appreciate it. I am having trouble with JavaScript code and do not know why it is not working. I have the code I think is right but it is not showing up in my browser when run. Any ideas? Also are there any other ways to read in an xml file using javascript? please list if so. Hi, i have a question, is JavaScript can read an external file? i have an ear file, can it read external file without the external file compile together in the ear file? can we do so? I am wanting to setup a dependant drop down where when a user clicks on the first drop down, it changes the contents of the second. The way I intend to do this is via a csv file. I have found several scripts but I have been unable to get them to work. Two of which a http://answers.yahoo.com/question/in...3114347AA7GYJ7 http://purbayubudi.wordpress.com/200...ng-javascript/ The first is the better option from what I can see. but the problem is readyState seems to be always 1. I am wondering if someone could perhaps give me a few moments to see if I can get this to work or suggest a better solution. I don't want to use a database approach, at least not yet. for some reason I am unable to read the file contents, or it could be something else, I added in some extra code to see where the script got to and I only get to see '1' on the output. Is there something else I have missed ? I am wanting to ready each line one by one and replace a phrase with another, then output the new line. I visit the 1.txt file in my browser and the file shows fine. var txtFile = new XMLHttpRequest(); txtFile.open("GET", "http://mysite.com/convertJS/1.txt", true); txtFile.onreadystatechange = function() { document.write("1<br>"); if (txtFile.readyState === 4) { // document is ready to parse. document.write("2<br>"); if (txtFile.status === 200) { // file is found document.write("3<br>"); allText = txtFile.responseText; lines = txtFile.responseText.split("\n"); document.write(lines + "<br>"); // I shall be altering the text in each line, search and replace, so only need to be able to read one line at a time. } } } txtFile.send(null); // close file. sorry that i used the quote method to post, but when i used [ code ] it corupted the text as the document . write lines?! Hi guys, here again with another question. I would parse a file, my js read fine an html file, but this one is an export from msword. I don't know if this is the problem. I can't change the file. here is how my js read: Code: <body> <input type='button' value='Load' onclick="doLoad()" /> <script type='text/javascript'> /* THIS IS THE FILE TO READ */ var fileToRead="test.html"; /* THIS FUNCTION IS TO READ THE HTML FILE */ function IO(U) {//LA MOD String Version. A tiny ajax library. by, DanDavis var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); X.open('GET', U, false ); X.setRequestHeader('Content-Type', 'text/html') X.send(''); return X.responseXML;} function doLoad(){ /* HERE IS THE CALL TO READ THE FILE */ var orari=(IO(fileToRead)); var arrTR=orari.getElementsByTagName('tr'); var arrTD_3nd_line=arrTR[1].getElementsByTagName('td'); var arr_p_1c_3l=arrTD_3nd_line[0].getElementsByTagName('p'); alert(arr_p_1c_3l[0].nodeValue); } </script> and here is a snip of the html i read: Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 10"> <meta name="Originator" content="Microsoft Word 10"> <link rel="File-List" href="orarioinvernale_file/filelist.xml"> <title>ORARI </title> <!--[if gte mso 9]><xml> <o:DocumentProperties> <o:Author>Ufficio Stampa</o:Author> <o:Template>Normal</o:Template> <o:LastAuthor>Ufficio Stampa</o:LastAuthor> <o:Revision>4</o:Revision> <o:TotalTime>1</o:TotalTime> <o:Created>2005-11-28T12:25:00Z</o:Created> <o:LastSaved>2006-01-13T10:24:00Z</o:LastSaved> <o:Pages>1</o:Pages> <o:Words>762</o:Words> <o:Characters>4344</o:Characters> <o:Company>mycomp</o:Company> <o:Lines>36</o:Lines> <o:Paragraphs>10</o:Paragraphs> <o:CharactersWithSpaces>5096</o:CharactersWithSpaces> <o:Version>10.2625</o:Version> </o:DocumentProperties> </xml><![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:SpellingState>Clean</w:SpellingState> <w:GrammarState>Clean</w:GrammarState> <w:HyphenationZone>14</w:HyphenationZone> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--> <style> <!-- /* Font Definitions */ @font-face {font-family:"Comic Sans MS"; panose-1:3 15 7 2 3 3 2 2 2 4; mso-font-charset:0; mso-generic-font-family:script; mso-font-pitch:variable; mso-font-signatu 647 0 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} span.SpellE {mso-style-name:""; mso-spl-e:yes;} span.GramE {mso-style-name:""; mso-gram-e:yes;} @page Section1 {size:595.3pt 841.9pt; margin:70.85pt 2.0cm 2.0cm 2.0cm; mso-header-margin:35.4pt; mso-footer-margin:35.4pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Tabella normale"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--> <meta http-equiv="Content-Language" content="it"> <!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="4098"/> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout></xml><![endif]--> </head> <body style="" bgcolor="#ccffff" lang="IT"> <div class="Section1"> <div style="text-align: center;"> </div> <table class="MsoNormalTable" style="width: 100%;" border="1" cellpadding="0" height="1733" width="100%"> <tbody> <tr style="height: 75.75pt;"> .... and so on.... the problem is that my error console (from firefox) when i click my read button (not when i load the page) gives me errors: Code: sintax error: source: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> if i comment this it give me: Code: missing tag </meta> source: </head> and if i put Code: </meta> another meta is required and so on. But i can't modify the source i read, so is there a way to read this file? Many thanks Backit I'm working on stripping specific HTML tags in order to do a selected string comparison. Stripping the tag, done. Re-calibrating the selection start and end points, done. Capturing the tags for re-insertion, done. Getting the start point of the tag, done. What I am having trouble with is finding the content between the tag so that I can put it back after altering the selected text. <span class="something">find this content!</span> Any ideas are more than welcome! Thanks, Hi I need to produce some code to get the contents of the particular TD I am running a function in. I have the following code which works fine up the point of wanting to delete a row. In order to make it work again I have to go into the code and adjust all the numbers in the functions (not sure that's the correct terminology) Code: <head> <script language='javascript'> exchRate = '1.12'; function gettheUKP(which) { // This is the line I think I need to change var x = document.getElementsByTagName("span")[which].innerHTML; var s = Math.round(x / exchRate); document.write('(£' + s + ')') } </script> </head> <body> <table cellspacing="0" cellpadding="0" class="charges"> <tr> <td><strong><span>368</span>€</strong> <script type="text/javascript">gettheUKP(0);</script></td> </tr> <tr> <td><strong><span>471</span>€</strong> <script type="text/javascript">gettheUKP(1);</script></td> </tr> <tr> <td><strong><span>598</span>€</strong> <script type="text/javascript">gettheUKP(2);</script></td> </tr> <tr> <td><strong><span>720</span>€</strong> <script type="text/javascript">gettheUKP(3);</script></td> </tr> <tr> <td><strong><span>998</span>€</strong> <script type="text/javascript">gettheUKP(4);</script></td> </tr> </table> </body> So in essence I think I need to change the highlighted line to say "get the contents on the span within this TD". Is this possible? Thanks Hey all, Hopefully I can explain this so it makes sense. I have a div with some buttons in it going horizontally. Kind of like a top menu of sorts. The div is of a fixed width and these buttons are spaced evenly across it. What I would like to do (and the hardest part is finding the right words for all this) is add more buttons. But because of the limited space I want to leave the current buttons as they are but provide a way, either by hovering over the left or right end of the div ideally or clicking << or >> types of links to 'cycle' through the buttons. So end result would be the user would see lets say 5 buttons upon load but be able to access the other buttons by cycling/scrolling left or right. It would be great if the buttons would loop around so they would never reach the 'end' going left or right. Does this make sense? I haven't been able to find anything close to this but that could possibly be because I am not sure what to call it. Thanks for any help! Hi, How I build a table based on a list under a <DIV></DIV> like this? printing: 1 and: 3 typesetting: 2 industry: 2 has: 2 been: 1 s: 1 standard: 1 ever: 1 since: 1 1500s: 1 when: 1 an: 1 unknown: 1 printer: 1 took: 1 a: 2 galley: 1 type: 3 scrambled: 1 Regards Bob Hi all! First post here (obviously)... I seem to have an issue with getting javascript to recognize the value of a variable. Code: for(var m=1; m<=86; m++) { var currgroup = "group" + m; alert(currgroup); if(currgroup.Contains(point)) { var userNG = m; var foundNG = true } else { var foundNG = false; } } If you see where I alert the value of currgroup, it displays "Group1" as it should... so the next line should be Group1.Contains(point); but it errors out as "currgroup.Contains(point) is not a function". If however I hard code it with Group1.Contains(point) it works fine. Is there a way to tell JS explicitly that I want it to use the variable contents instead of the name that I'm missing? Thanks a bunch for any and all help, Matt Hi.. Im currently working on my project and I have a big problem with changing the layout of tabs. Basically, when I launch my GUI, it will display "No Data Available" in my view tab. In my Add tab, I have the admin panel whereby the user enters the data to be stored and display. the problem now lies when displaying. It is a juke ringtone gallery whereby when there are data being stored in the database, it will display the following in a Gridlayout; I applied the ChangeListener to the JTabbedPane which is tp. I added tabs using tp.add. I declared this in my ChangeListener: if(tp.getSelectedIndex() == 0) createViewPanel(); else if(tp.getSelectedIndex()==1) createAddPanel(); where createViewPanel() is for the View tab and createAddPanel() is for the Add tab. i also called the respective tabs using frameView = (JPanel)tp.getComponent(0); to get the layout for View tab and frameAdd = (JPanel)tp.getComponent(1); to get the layout for the Add tab. What methods need I use so that I can display the different data in the specific GridLayout to be displayed in the View tab or what other ways can I do it? Hey. I have been pulling out my hair trying to do this. I am printing out the contents of a DIV tag but it does not print the css with it. Just the basic text For example: .testDiv { border: 1px solid #0F0; margin-right: 300px; text-decoration: underline; font-weight: bold; } The print does not change the text according to the css. Why is this? Thanks in advance for any help. Marc Hi guys I was wondering if its possible to add contents within a div to a javascript array. Something like this: Code: <script type="text/javascript"> function returnArray() { var divContent = document.getElementById('images'); var matchImage = divContent.match(/<img/); /* add images into array somehow */ alert('This is ' +ImageArray[1]); } </script> <div id="images"> <img src="image1.jpg" alt="image" /> <img src="image2.jpg" alt="image" /> <img src="image3.jpg" alt="image" /> </div> Thanks for any info. |