JavaScript - Converting Txt File To Array Problems.
I'm just fooling around trying to make a simple ChatterBot.
And I'm using IE to make a simple database of Answers and Sentences. Problem is it's acting odd when it's spliting into an array. In Answer.txt I have "Hello, *UserName*." (Which I will use the replace() to change that later.) And in Sentence.txt I have.... Hello. I always use "Hello." as the message. It's suppose to compare the message with the sentences... Both lowercased and symbols removed. But it doesn't seem to work just right when coverting the Text file to array. Code: function SearchForAnswers(Message) { var fso = new ActiveXObject("Scripting.FileSystemObject"); var a, ForReading, file; ForReading = 1; file = fso.OpenTextFile("Sentence.txt", ForReading, false); var AllSentences = file.ReadAll(); Sentences = AllSentences.split("\n"); file.Close(); var fso = new ActiveXObject("Scripting.FileSystemObject"); var a, ForReading, file; ForReading = 1; file = fso.OpenTextFile("Answer.txt", ForReading, false); var AllAnswers = file.ReadAll(); Answers = AllAnswers.split("\n"); file.Close(); var count = 0; var found = -1; var TheAnswer; while (count < Sentences.length) { var checksent = Sentences[count].toLowerCase(); var checkmess = Message.toLowerCase(); alert("Message",checksent.replace(/\W/i, "")+" "+checkmess.replace(/\W/i, "")+" Count:"+count+" "+found); if (checksent.replace(/\W/i, "") == checkmess.replace(/\W/i, "")){ var found = count; TheAnswer = Answers[count]; } count = count + 1 } if (found == -1) { var prompted = prompt("Answer please:", ""); SaveSentence(Message); SaveAnswer(prompted); TheAnswer = prompted; } return TheAnswer; } Resolved: I am just going to import .js files into my page. And just edit the external .js files. 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) Hello everybody, i need some help to resolve a problem that i am not seeing in code, no errors, but the result is the same , none : var a = new Array() for (var i = 0; i < k; i++) { a[i] = new Array(20) } for (var i = 0; i < k; i++) { for (j = 0; j < k; j++) { a[i][j] = 0 ; } } a[0][0] = 1 ; a[1][0] = 1 ; a[1][1] = 1; for ( i = 0; i < k; i++) { a[i][0] = 1; } for (var i = 2; i > k; i++) { while (a[i - 1][y + 1] != 0) { a[i][s] = a[i - 1][s - 1] + a[i - 1][s]; } s = s + 1 ; a[i][s] = 1 ; } for (i = 0; i < k; i++) { for (var j = 0; j > k; j++) { document.write(a[i][j]); } document.writeln(); } I've done in free pascal same thing (it was much easier) and i want it in javascript. So if someone knows how to resolve this , please help me to understand how. http://en.wikipedia.org/wiki/Pascal%27s_triangle First, this is my first time coding anything seriously. I've only dabbled here and there, but no more. Any and all tips are appreciated. I have a program that's designed to calculate total enemy health. Its does this by taking user input on the level and enemy name. The enemy level input determines the base health stat. The enemy name determines the percent multiplier. This all runs on an onClick() event. I originally had this all in my <head> in my html code. It ended up being more code than I expected. So i made it an external javascript file. I did remove the <script></script> tags, I believe I have the file properly linked using the src attribute. I'm at my wits end. This is my second day of trying. So here is the javascript code. Code: // JavaScript Document var series; var index; var indexValue; var base; var distance; var sequence; var enemyLv; var enemyPct; var enemyHp; var vOneFive; var wrongAlert; var enemyList; var enemyName; var enemyInput; var enemyLevel; var a = enemyLEVEL(); var b = enemyPCT(); var totalHp = a * b; //calculates enemyHp and enemyPct function calc(enemyHp, enemyPct) { a = enemyLEVEL(); b = enemyPCT(); totalHp = a * b; if(a && b !== -1){ document.getElementById('enemyTotalHp').innerHTML = totalHp; } else{ alert(wrong); } } //determines base stat for enemyHp function enemyLEVEL(enemyHp){ enemyLv = document.getElementById('enemyLevel').value; var enemyLvValue = enemyLv * 1; wrongAlert = "BURGA!"; //determines which HP sequence to run if(enemyLvValue <= 5){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '12345'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 70; distance = 13; sequence = base + (indexValue * distance); enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 10){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '5678910'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 122; distance = 24.8; sequence = base + (indexValue) * distance; enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 15){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '101112131415'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 246; distance = 50; sequence = base + ((indexValue)/2) * distance; enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 20){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '151617181920'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 496; distance = 129.8; sequence = base + ((indexValue)/2) * distance; enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 45){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '2021222324252627282930313233343536373839404142434445'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 1145; distance = 120; sequence = base + ((indexValue)/2) * distance; enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 50){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '454647484950'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 4145; distance = 133; sequence = base + ((indexValue)/2) * distance; enemyHp = sequence; return enemyHp; } else if (enemyLvValue <= 75){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '5051525354555657585960616263646566676869707172737475'; index = series.lastIndexOf(enemyLvValue); indexValue = index * 1; base = 4810; distance = 75; sequence = base + ((indexValue)/2) * distance; enemyHp = sequence; return enemyHp; } else if (enemyLvValue <= 99){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series = '75767778798081828384858687888990919293949596979899'; index = series.lastIndexOf(enemyLvValue); indexValue = index * 1; base = 6685; distance = 70; sequence = base + ((indexValue)/2) * distance; enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 110){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series ='100101102103104105106107108109110'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 8435; distance = 70; sequence = base + ((indexValue)/3) * distance; enemyHp = sequence; return enemyHp; } else if(enemyLvValue <= 170){ enemyLv = document.getElementById('enemyLevel').value; enemyLvValue = enemyLv * 1; series ='110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170'; index = series.indexOf(enemyLvValue); indexValue = index * 1; base = 9130; distance = 70; sequence = base + ((indexValue)/3) * distance; enemyHp = sequence; return enemyHp; } else{ alert(wrongAlert); } } //determines enemyPct variable function enemyPCT(enemyPct){ enemyInput = document.getElementById('enemyUser').value; enemyName = enemyInput; enemyList = new Array(); enemyList[1] = "Vahra Go Vahra Delsaban Golmoro Goshin Fulyen Curtz SEED-Guardian (Tw) Special Ops (Assault) Bysha type-Koh21 Bysha type-Otsu32 Gohma Dilla Gohma Methna GSM-05 Seeker GSM-05B Bomalta GSM-05M Tirentos YG-01K BUGGE YG01U BUGGES YG-01Z BUG Deljaban Orcdillan Sendillan";//100% hp // enemyList[2] = "Ageeta Sageeta Rogue Wikko Special Ops (Vobis)"; //60% hp // enemyList[3] = "Pannon Bel Pannon Galdeen Kudetob Nava Ludda Naval Zoona Shagreece Lapucha Polty Badia Go Bajilla"; //70% hp // enemyList[4] = "Armed Servant (Obme) Armed Servant (Ozuna) Distova Volfu Olgohmon Bul Buna";//90% hp // enemyList[5] = "Koltova Kakwane Ollaka Rappy Polec AMF Heavy Infantry Armed Servant (Basta) SEED-Guardian (Kn) Delp Slami";//130%hp // enemyList[6] = "Booma Go Booma Jigo Booma Jishagara Vanda Vanda Merha Vanda Orga Rogue (Ogg) SEED-Guardian (Sa)";//150%hp // enemyList[7] = "Rogue (Jasse) Rogue Mazz Special Ops (Kanohne) Special Ops (Solda)";//180%hp // enemyList[8] = "Komazli Bead Groode Dilla Griena Grinna Bete C";//200%hp // enemyList[9] = "Bal Soza Gaozoran";//230%hp; // enemyList[10] = "Shinowa Hidoki" ;//250% hp // enemyList[11] = "Jaggo Jaggo Acte Jaggo Amure Jaggo Sonichi Zamvapas Galvapas Do Vol No Vol";//260% hp // enemyList[12] = "Mizura Gainozeros Grass Assassin Ubakrada Rappy Rappy Amure Rappy Latan Rappy Noel Rappy Paska";//300% hp // enemyList[13] = "Kamatoze Bafal Bragga";//320% hp // enemyList[14] = "Jarba Polavohra Hiru Vol Delnadian SEED-Ardite Zasharogan Lutus Jigga Orgdus Svaltus";//350% hp // enemyList[15] = "Tengohg Bil De Melan Bil De Vear Drua Gohra Kog Nadd Dilnazen Jusnagun SEED-Argine SEED-Vance SEED-Vitace Ryugtass";//400% hp // enemyList[16] = "Carriguine SEED-Venas";//450% hp // enemyList[17] = "Rappy Gugg Renvolt Magashi Grinna Bete S";//500% hp // enemyList[18] = "Rappy Igg";//550% hp // enemyList[19] = "Darbelan Alfort Tylor";//600% hp // enemyList[20] = "Kagajibari";//630% hp // enemyList[21] = "Gol Dova";//650% hp // enemyList[22] = "Zoal Goug"; //700% hp // enemyList[23] = "De Rol Le";//1000% hp // enemyList[24] = "SEED-Magashi";//1100% hp // enemyList[25] = "Alterazgohg Onmagoug";//1350% hp // enemyList[26] = "Adahna Dimmagolus Dark Falz";//1450% hp // enemyList[27] = "De Ragan Magas Maggahna";//1500% hp // enemyList[28] = "Dulk Fakis Mother Brain";//1650% hp // enemyList[29] = "De Ragnus";//1720% hp var one = enemyList[1]; var two = enemyList[2]; var three = enemyList[3]; var four = enemyList[4]; var five = enemyList[5]; var six = enemyList[6]; var seven = enemyList[7]; var eight = enemyList[8]; var nine = enemyList[9]; var ten = enemyList[10]; var eleven = enemyList[11]; var twelve = enemyList[12]; var thirteen = enemyList[13]; var fourteen = enemyList[14]; var fifteen = enemyList[15]; var sixteen = enemyList[16]; var seventeen = enemyList[17]; var eighteen = enemyList[18]; var nineteen = enemyList[19]; var twenty = enemyList[20]; var twentyone = enemyList[21]; var twentytwo = enemyList[22]; var twentythree = enemyList[23]; var twentyfour = enemyList[24]; var twentyfive = enemyList[25]; var twentysix = enemyList[26]; var twentyseven = enemyList[27]; var twentyeight = enemyList[28]; var twentynine = enemyList[29]; // var indexOne = one.indexOf(enemyName); var indexTwo = two.indexOf(enemyName); var indexThree = three.indexOf(enemyName); var indexFour = four.indexOf(enemyName); var indexFive = five.indexOf(enemyName); var indexSix = six.indexOf(enemyName); var indexSeven = seven.indexOf(enemyName); var indexEight = eight.indexOf(enemyName); var indexNine = nine.indexOf(enemyName); var indexTen = ten.indexOf(enemyName); var indexEleven = eleven.indexOf(enemyName); var indexTwelve = twelve.indexOf(enemyName); var indexThirteen = thirteen.indexOf(enemyName); var indexFourteen = fourteen.indexOf(enemyName); var indexFifteen = fifteen.indexOf(enemyName); var indexSixteen = sixteen.indexOf(enemyName); var indexSeventeen = seventeen.indexOf(enemyName); var indexEighteen = eighteen.indexOf(enemyName); var indexNineteen = nineteen.indexOf(enemyName); var indexTwenty = twenty.indexOf(enemyName); var indexTwentyOne = twentyone.indexOf(enemyName); var indexTwentyTwo = twentytwo.indexOf(enemyName); var indexTwentyThree = twentythree.indexOf(enemyName); var indexTwentyFour = twentyfour.indexOf(enemyName); var indexTwentyFive = twentyfive.indexOf(enemyName); var indexTwentySix = twentysix.indexOf(enemyName); var indexTwentySeven = twentyseven.indexOf(enemyName); var indexTwentyEight = twentyeight.indexOf(enemyName); var indexTwentyNine = twentynine.indexOf(enemyName); // var test = "YOU CAN DO IT LITTLE NICKY! BITE HIS ****ING HEAD OFF!"; // if(indexOne != -1){ var enemyPctValue = 1; enemyPct = enemyPctValue; return enemyPct; } // else if(indexTwo != -1){ var enemyPctValue = 0.6; enemyPct = enemyPctValue; return enemyPct; } // else if(indexThree != -1){ var enemyPctValue = 0.7; enemyPct = enemyPctValue; return enemyPct; } // else if(indexFour != -1){ var enemyPctValue = 0.9; enemyPct = enemyPctValue; return enemyPct; } // else if(indexFive != -1){ var enemyPctValue = 1.3; enemyPct = enemyPctValue; return enemyPct; } // else if(indexSix != -1){ var enemyPctValue = 1.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexSeven != -1){ var enemyPctValue = 1.8; enemyPct = enemyPctValue; return enemyPct; } else if(indexEight != -1){ var enemyPctValue = 2; enemyPct = enemyPctValue; return enemyPct; } else if(indexNine != -1){ var enemyPctValue = 2.3; enemyPct = enemyPctValue; return enemyPct; } else if(indexTen != -1){ var enemyPctValue = 2.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexEleven != -1){ var enemyPctValue = 2.6; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwelve != -1){ var enemyPctValue = 3; enemyPct = enemyPctValue; return enemyPct; } else if(indexThirteen != -1){ var enemyPctValue = 3.2; enemyPct = enemyPctValue; return enemyPct; } else if(indexFourteen != -1){ var enemyPctValue = 3.5; enemyPct = enemyPctValue; return enemyPct; } else if (indexFifteen != -1){ var enemyPctValue = 4; enemyPct = enemyPctValue; return enemyPct; } else if(indexSixteen != -1){ var enemyPctValue = 4.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexSeventeen != -1){ var enemyPctValue = 5; enemyPct = enemyPctValue; return enemyPct; } else if(indexEighteen != -1){ var enemyPctValue = 5.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexNineteen != -1){ var enemyPctValue = 6; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwenty != -1){ var enemyPctValue = 6.3; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyOne != -1){ var enemyPctValue = 6.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyTwo != -1){ var enemyPctValue = 7; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyThree != -1){ var enemyPctValue = 10; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyFour != -1){ var enemyPctValue = 11; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyFive != -1){ var enemyPctValue = 13.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentySix != -1){ var enemyPctValue = 14.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentySeven != -1){ var enemyPctValue = 15; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyEight != -1){ var enemyPctValue = 16.5; enemyPct = enemyPctValue; return enemyPct; } else if(indexTwentyNine != -1){ var enemyPctValue = 17.2; enemyPct = enemyPctValue; return enemyPct; } else{ alert(enemyName); } } and this is the html code 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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PSUpedia Enemy HP Calculator</title> <script type="text/javascript" src="hp_calc.js"> </script> </head> <body> <form> Enemy Level: <input type="text" id="enemyLevel"></br> Enemy Name: <input type="text" id="enemyUser"></br> <p>EnemyHp: <b id="enemyTotalHp"> </b></p></br> <input type="button" value="CLICK!" onClick="calc();"> </form> </body> </html> I have a multidimensional associative array in JS which contains category information for a navigation. It's actually set up through PHP, which pulls the info from a database then translates it into JS. I know it's set up correctly since I can see it in the source once the page is rendered. However, accessing this array causes external JS code (from my Slimbox files) to be displayed in the nav. It's as if the array is set up right, but accessing the elements brings in Slimbox code as the elements. It's very strange. You can see the effect here. It's the secondary nav under Home, Featured Items, etc. Each token that is clickable is separated by an asterisk: http://www.mchonejewelry.com/test/newsite/community.php Notice the other page's navs work great! Try the jewelry nav on the Home page and the categories are set up fine. This is because other pages do not link to Slimbox. (And no, the site aint pretty because I wanted to get the hard stuff working before I work on the look) Any clue what's going on? I have no idea how this can be happening. I've heard of jQuery interfering with Slimbox and Lightbox, but my nav is not using jQuery! I'm trying to display a random message by making an array through values in an XML file and then displaying the info on the page. i want to have it so that a new message is generated each time the button is clicked with no repeats. here is what i have Code: function initial() { var xmlDoc; // First option for internet explorer 5 and 6, second option for firefox, internet explorer 7+ window.ActiveXObject ? xmlDoc = new ActiveXObject("Microsoft.XMLDOM") : xmlDoc = document.implementation.createDocument("","",null); xmlDoc.async = false; xmlDoc.load("quotes.xml"); // take the quote and author information from the xml file var pQuotes = xmlDoc.getElementsByTagName('quote'); var pAuthors = xmlDoc.getElementsByTagName('author'); // create arrays var aQuotes = new Array(); var aAuthors = new Array(); // put the quotes into the array for (i=0; i<pQuotes.length; i++) { aQuotes.push(pQuotes[i].childNodes[0].nodeValue); alert(pQuotes); } // put the authors into the array for (i=0; i<pAuthors.length; i++) { aAuthors.push(nAuthors[i].childNodes[0].nodeValue); } // Loop through arrays and print their quotes and authors for (var s in aQuote) { alert(aQuotes[s]); alert(aAuthors[s]); } } var Q = aQuotes.length; var quoteText = document.getElementById('aQuotes'); var authorText = document.getElementById('aAuthors'); var randomquote = Math.floor(Q * Math.random()); function quoter() { var randomquote2 = randomquote; while (randomquote == randomquote2) { randomquote = Math.floor(Q * Math.random()); } document.getElementById('quoteText').innerHTML = aQuotes[randomquote]; document.getElementById('authorText').innerHTML = " - " +aAuthors[randomquote]; quote.innerhtml = aQuotes[randomquote]; author.innerhtml = aAuthor[randomquote]; } navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false); </script> XML Code: <?xml version="1.0" encoding="utf-8" ?> <quotes> <theQuote> <quote>Over the years your bodies become walking autobiographies, telling friends and strangers alike of the minor and major stresses of your lives.</quote> <author>Marilyn Ferguson</author> </theQuote> <theQuote> <quote>In reality, serendipity accounts for one percent of the blessings we receive in life, work and love. The other 99 percent is due to our efforts.</quote> <author>Peter McWilliams</author> </theQuote> <theQuote> <quote>The secret of joy in work is contained in one word - excellence. To know how to do something well is to enjoy it.</quote> <author>Pearl Buck</author> </theQuote> <theQuote> <quote>The discovery of a new dish does more for human happiness than the discovery of a new star.</quote> <author>Anthelme Brillat-Savarin</author> </theQuote> <theQuote> <quote>When I was born I was so surprised I didn't talk for a year and a half.</quote> <author>Gracie Allen</author> </theQuote> </quotes> HTML Code: <body onload="initial()"> <div id="container"> <div id="header"> <h1>quotes</h1> </div> <div id="content"> <span id="quoteText"></span> <span id="authorText"></span> <br/> <br/> <input type="button" value="Click here for another quote" onclick="quoter()"/> </div> Code: function sortArrayGo(startingArray, sortNum) { var holder = startingArray; //holder.push(startingArray[0]); var sortedArray = new Array(); var endNum = startingArray.length; //indexPlace=0; for(n = 0; n < endNum; n++) { var min = 0; for(m=1; m<holder.length; m++) { alreadyMin = holder[min][sortNum]; possibleMin = holder[m][sortNum]; if(alreadyMin > possibleMin) { min = m; } } thisLine = startingArray.splice(min, 1); for(cellCount = 0; cellCount < tablePos.length; cellCount++) { colNum = tablePos[cellCount]; dataToInsert = thisLine[colNum]; document.getElementById('dataTable').rows[n].cells[cellCount].innerHTML = dataToInsert; } sortedArray.push(thisLine); } return sortedArray; } i'm sorting a 2d array by whatever column i specify. i know from using firebug that my sorting is working, but it's getting and displaying undefined for dataToInsert and inside of my table, but sortedArray winds up sorted correctly. this is my first time attempting this operation. I want to take some javascript out of my html. here is the original: Code: <!--div#9begin--><div style="float: left; height: 166px; width: 1px;"> <img alt="" src="images/border_short_vert.png" height="169" width="1" /> </div><!--div#9end--> <!--div#10begin--><div style="width: 225px; height: 155px; float: left; height: 16px; background-color: #000000; color: #FFFFFF; margin-left: 0px; margin-bottom: 0px; font-size: small; padding-left: 0px;"> <img alt="" src="images/news.png" height="16" width="222" /><!--div#11begin--><div style="width: 225px; height: 155px; float: left;"></div><!--div#11end--> <script type="text/javascript">// <![CDATA[if(document.layers){document.write('<ilayer id="ns4div" width="'+swidth+'" height="'+sheight+'" bgcolor='+sbcolor+'><div id="ns4div1" width="'+swidth+'" height="'+sheight+'" onmouseover="sspeed=0;" onmouseout="sspeed=rspeed"></div></ilayer>')} if(document.getElementById||document.all){document.write('<div style="position:relative;overflow:hidden;width:'+swidth+'px;height:'+sheight+'px;clip:rect(0 '+swidth+'px '+sheight+'px 0);background-color:'+sbcolor+';" onmouseover="sspeed=0" onmouseout="sspeed=rspeed"><div id="iens6div" style="position:relative;width:'+swidth+'px;"></div></div>');} // ]]> </script> </div><!--div#10end--> and here's my attempt: Code: <div style="width: 225px; height: 155px; float: left; height: 16px; background-color: #000000; color: #FFFFFF; margin-left: 0px; margin-bottom: 0px; font-size: small; padding-left: 0px;"> <img alt="" src="images/news.png" height="16" width="222" /><script type="text/javascript" src="docwrite.js"></script> <div style="width: 225px; height: 155px; float: left;"></div> </div> along with my js: Code: if(document.layers) {document.write('<ilayer id="ns4div" width="'+swidth+'" height="'+sheight+'" bgcolor='+sbcolor+'><div id="ns4div1" width="'+swidth+'" height="'+sheight+'" onmouseover="sspeed=0;" onmouseout="sspeed=rspeed"></div></ilayer>')} if(document.getElementById||document.all) {document.write('<div style="position:relative;overflow:hidden;width:'+swidth+'px;height:'+sheight+'px;clip:rect(0 '+swidth+'px '+sheight+'px 0);background-color:'+sbcolor+';" onmouseover="sspeed=0" onmouseout="sspeed=rspeed"> <div id="iens6div" style="position:relative;width:'+swidth+'px;"></div></div>');} what is the problem? Have I not performed the extraction properly? Hi Guys, I dont use JS much, I did take it in school years ago but cant remember much of it, what Im trying to do is quite simple, just a random quote gen but I want it to get the quots from a separate file as the quotes will appear on every page so I dont want to have to edit each page every time a new one needs to be added. I just found a simple quote generator online that I want to modify to call the array from a different file, the gen looks like this: var Quotation=new Array() // do not change this! Quotation[0] = "Time is of the essence! Comb your hair."; Quotation[1] = "Sanity is a golden apple with no shoelaces."; Quotation[2] = "Repent! The end is coming, $9.95 at Amazon."; Quotation[3] = "Honesty blurts where deception sneezes."; Quotation[4] = "Pastry satisfies where art is unavailable."; Quotation[5] = "Delete not, lest you, too, be deleted."; Quotation[6] = "O! Youth! What a pain in the backside."; Quotation[7] = "Wishes are like goldfish with propellors."; Quotation[8] = "Love the river's \"beauty\", but live on a hill."; Quotation[9] = "Invention is the mother of too many useless toys."; var Q = Quotation.length; var whichQuotation=Math.round(Math.random()*(Q-1)); function showQuotation(){document.write(Quotation[whichQuotation]);} showQuotation(); </script> If anyone could help me get that array into another file and call it from that script I would really appreciate it. Thanks guys! Hi, 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 Hi, I have an external JS file with an array of menu links called (menu.js). The code is shown below: Code: <script> var menuItem = new Array(); menuItem[0]="index.html"; menuItem[1]="welcome_home/index.html"; menuItem[2]="key_resources/index.html"; menuItem[3]="deployment_assistance/index.html"; menuItem[4]="benefits/index.html"; menuItem[5]="services/index.html"; menuItem[6]="counties/index.html"; menuItem[7]="transitional_help/index.html"; menuItem[8]="women_vets/index.html"; menuItem[9]="iowa_heroes/index.html"; menuItem[10]="http://www.iowava.org/vetcemetery/"; menuItem[11]="http://ivh.iowa.gov"; menuItem[12]="../forms/index.html"; menuItem[13]="https://va.iowa.gov/reselig/"; menuItem[14]="links2/index.html"; menuItem[15]="Whats_New/index.html"; menuItem[16]="contact_us/index.html"; function Page(which){ location.href = menuItem[which]; } </script> I have called the javascript in my websites main page (index.html), but it does not work. The html code is shown below: Code: <script type ="text/javascript" src="menu.js"></script> <a href="javascript:void(0);" onclick="Page(0);">Home</a> <a href="javascript:void(0);" onclick="Page(1);">Welcome Home</a> <a href="javascript:void(0);" onclick="Page(2);">Key Resources</a> The javascript for the array of menu links works when it is inserted directly within the html file, but when it remains external and is called in index.html it does not work. I am not sure why this is the case. Could anyone give some pointers? Thanks, -Mike I have a Java Script that displays a Daily Affirmation on a webpage from an array of 365 Affirmations within the same html webpage file. This makes the html file size way too large. So I would like for the java script to read the array from an external text file on the same website server. Please provide me the Code to insert within my script which will read the array from an external text file - and also the format that the array of 365 lines must be typed into the external text file. I am not a Javascript Programmer - and know nothing about Javascript, so please write your reply at my (lack of) knowledge Level. Thank you in advance! Here is the code I am using. <script language=javascript> <!-- Date.prototype.getDOY = function() {var onejan = new Date(this.getFullYear(),0,1); return Math.ceil((this - onejan) / 86400000);} var today = new Date(); var DOY = today.getDOY(); var HL=new Array() //Configure the following array to hold the 365 HLs for each day of the year HL[1]='HL 1 goes here' HL[2]='HL 2 goes here' HL[3]='HL 3 goes here' // Jump to Day 224 of the year for this example only HL[224]='I am surrounding myself with positive and supportive people. When I nurture relationships that give me energy and enjoyment, I mirror my beliefs that I deserve such gifts.' HL[225]='Today I affirm my own worth and value, and discover that the world agrees with me.' HL[226]='I am sowing seeds based on a healthy belief in my own self-worth. My life is flourishing with growing love, contentment, and exciting possibilities.' HL[227]='HL 227 goes here' HL[228]='HL 228 goes here' // Jump to last HL for Day 365 of year - for this example only HL[365]='HL 365 goes here' document.write(HL[DOY]) //--> </script> hello everyone im coding a website at the moment using aspx and i have came to a snag. im trying to make a dropdown list which is populated by an array call my chaletDetails.aspx page - each item in the array will be directed to this page. ive diffled abit, but the more i do the more im afraid of breaking something lol. if someone could maybe look at the code and show me how this could be achieved id appriciate it very much. The code i have for my dropdown list is as follows... this is in divResorts.js var resorts = new Array("Adelboden", "Auron", "Oppdal", "Las Lenas", "La Pierre St Martin", "Val Ceneis", "Les Menuires", "Champex-Lac", "Puy St Vincent"); function populateSelectList() { var mystring = document.getElementById('divResort').innerHTML mystring += "<h4>Our Resorts</h4><select name='resorts'>" mystring += "<option>Choose a Resort</option>" for (i = 0; i < resorts.length; i++) { mystring += "<option value='" + resorts[i] + "'>" mystring += resorts[i] + "</option>" } mystring += "</select>" mystring += "<h4>Our Chalets</h4>" document.getElementById('divResort').innerHTML = mystring } within the body i have this called as: <div id="divResort"> <script type="text/javascript">populateSelectList();</script> </div> Not sure if this should be in the ASP forum or this one (probably could go either), so here goes. I don't know hardly any - probably best to say dont know any at all - javascript. However I have a need to convert some code from asp to js. the code is checking to see if certain variables are null and if so counting how many of those variables are as such. the ASP looks like this: Code: <% Dim counter counter = 0 if a <> "" then classcounter = classcounter + 1 if b <> "" then classcounter = classcounter + 1 if c <> "" then classcounter = classcounter + 1 if d <> "" then classcounter = classcounter + 1 if e <> "" then classcounter = classcounter + 1 if f <> "" then classcounter = classcounter + 1 %> I need to know how to convert this to js so I can show this number to the user without sending data to the server first. I have searched the net for something similar but I am not having much luck finding any examples that do what I would like it to do (maybe this is not something that can/should be done in js?). I hope this makes sense...thanks! I am trying to convert following pesudocose to javascript. Dont know where am i messing it up. Any help will be great PSEUDOCODE: Program TaxCalc Read purchase If(purchase>=0 AND purchase<=7) tax=0 else if(purchase>=8 AND purchase<=21) tax=1 else if(purchase>=22 AND purchase<=35) tax=2 else if(purchase>=36 AND purchase<=49) tax=3 else if(purchase>=50 AND purchase<=64) tax=4 else if(purchase>=65 AND purchase<=78) tax=5 else if(purchase>=79 AND purchase<=92) tax=6 else if(purchase>=93 AND purchase<=99) tax=7 else print error the number should be >=0 and <=99 print tax End Program JAVASCRIPT that I have so far <html> <body> <script type="text/javascript"> // Program name: Tax Calculation // Purpose: Find tax for amounts under $1 // Name: Niral Patel // Date: 2/15 START //Declare variables var purchase; //Amount to be entered by user var tax; //Tax to be calculated //Welcome the user //Ask them to enter an amount less than a dollar document.write("This program will help you calculate tax" + BR); document.write("Please enter the amount, the amount has to be less than a dollar" + BR); purchase = prompt("Enter the amount for which tax is to be calculated:",ES); purchase = parseInt(purchase) if (purchase >= 0 ) { tax = 0 } else if (purchase >= 7 ) { tax = 0 } else if (purchase >= 21 ) { tax = 2 } else if (purchase >= 35 ) { tax = 3 } else if (purchase >= 49) { tax = 4 } else if (purchase >= 64 ) { tax = 5 } else if (purchase >= 78 ) { tax = 6 } else if (purchase >= 92 ) { tax = 7 } else if (purchase >= 93 ) { tax = 6 } else if (purchase >= 99 ) { tax = 6 } else { document.write("Number should be between 0 and 99." +BR); } display tax; Stop </script> </body> </html> Hi, I was wondering - how can I convert something that a user draws inside canvas? Say, they draw a square, how could I convert that to print "You have drawn a square" - this would be posted to a text file. Thanks in advance. Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> </head> <script type = "text/javascript"> var strCurrency = new Array(6); strCurrency[0]="Pound"; strCurrency[1]="Marc"; strCurrency[2]="Franc"; strCurrency[3]="Dollar"; strCurrency[4]="Nordic"; strCurrency[5]="Fivesmon"; var dblExchangeRate = new Array(6); dblExchangeRate[0]="1"; dblExchangeRate[1]="74"; dblExchangeRate[2]="8.54"; dblExchangeRate[3]="6.25"; dblExchangeRate[4]="98.1"; dblExchangeRate[5]="1.32"; function convertNumbers { } </SCRIPT> I want to see what the function would look like to be able to convert into different currencies by using arrays. I know that this would be a useless way to do it as they are updated every day, but it's for personal education purposes. (Note: The website contained a textbox in which to enter the amount in pounds, a drop-down box allowing the user to select currency, a button to click to convert, and another text box showing amount of money after converting.) Any help is greatly appreciated. Hello, I need your help converting the code below from VBA to Javascript. I am basically attempting to export a recordset to an excel file. VBA: Code: '------------------------------------------------------- Public Sub ExportTOExcel() '------------------------------------------------------- Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks.Add Set oSheet = oBook.Worksheets(1) Dim j As Long Dim lngCol As Long Dim lngRow As Long Dim Field As Object rs.MoveFirst lngRow = 1 With oSheet Do While Not rs.EOF lngCol = 0 For Each Field In rs.Fields lngCol = lngCol + 1 .Cells(lngRow, lngCol) = Field.Value Next lngRow = lngRow + 1 rs.MoveNext Loop End With oBook.SaveAs "C:\Book5.xls" Workbooks.Open fileName:="C:\Book5.xls" End Sub Thanks for everyones help in advance, Cheers, J I am having an issue with converting my decimal to a percentage... any suggestions? Thanks - it's probably an easy solution, but I am lost below is my code (cost * .06) is where the issue is - Thanks! var cost = prompt("What is the cost of your purchase?", "") document.write("Return Value: "+cost,("<br />")); var salestax = cost * .06 document.write("Return Value: "+salestax,("<br />")); var total = cost + salestax document.write(cost + salestax); deleted
ok, quick and to the point. I have this... Code: <a href="www.mylink.com" class="myclass">click</a> and I wanna change it to a javascript function that can be called via the SAME tag, but with this link: Code: <a href="javascript: myfunction()">click</a> Here's what i have so far: Code: <head> <script type="text/javascript"> function myfunction() { window.location.href='www.mylink.com'; } </script> </head> <body> <a href="javascript: myfunction()">click</a> </body> this works to the point where it only opens the link, but now i can't figure out how to add the class to it. I tried adding it inside the body tag, but it doesn't work. So, I'm wondering if it's possible to set the class in the javascript. Thanks in advance for the help, I really appreciate it |