JavaScript - Array Memory Problem
Hi:
I was trying to post in flowplayer forum but... Anyway, the code below works fine when run with browser called from Coffee Cup HTML editor. Files are played as requested from button onClick events. All files called and all ref files are on hosting server. When I put this page on the server and click the first button, the files play fine. When I then click thge second button, the files called by the first button play again. If I then click the second button, the files called by button 2 play. If I then click button 5, the files called by button 2 play again. The previous config is somehow being stored somewhere and called. I have added this and that as you can see in the code to try to rid the old config but to no avail: playerConfig = null playerConfig.playlist =[]; should clear it. delete playerConfigplaylist[0]; Should clear it. and thinking it was bumped to [1] delete playerConfigplaylist[1]; Should clear that. So I think the old playerConfig.playlist should be gone but apparently not so. I have reviewed: jquery.min.js, flowplayer-3.2.6.min.js and flowplayer.playlist-3.0.8.js and cannot see any cause to this behavior though I am not a js ace. Any insight appreciated. Best regards <code> <script> var mediaPlayer = null; var playerConfig = null; function createPlayerConfig () { playerConfig = null; playerConfig = { log: { level: 'debug' } , play: null, plugins: { controls: { url: 'https://www.jala-mi.org/gmfiles/flowplayer.controls-3.2.5.swf', bottom: 0, height: 24, backgroundColor: "#2d3e46", backgroundGradient: "low", fontColor: '#ffffff', timeFontColor: '#333333', autoHide: 'never', play:true, volume:true, mute:true, time:true, stop:true, playlist:true, fullscreen:true, scrubber: true } } } }; function changePlaylist(newplaylist,no_of_items) { playerConfig.playlist = []; delete playerConfig.playlist[0]; for(i=0; i < no_of_items; i++) { playerConfig.playlist[i] = newplaylist; } delete playerConfig.playlist[1]; createPlayer(); // reload player with new playlist mediaPlayer.play(); // start the newly loaded clip sequence* } function createPlayer() { if(mediaPlayer != null) { if(mediaPlayer.isLoaded()) { mediaPlayer.unload(); mediaPlayer = null; } } $f("player", { src:"https://www.jala-mi.org/gmfiles/flowplayer-3.2.7.swf" }, playerConfig ); mediaPlayer = $f("player"); } function loadandplay(intro, movie) { var presentation = {url: movie , duration: 0, autoPlay: true, autoBuffering: false, fadeInSpeed: 8000, fadeOutSpeed: 5000, title: 'JPD 02/28/2011',playlist: [{url: intro , duration: 0, position: 0, fadeInSpeed: 5000, fadeOutSpeed: 8000, autoPlay: true, autoBuffering: true, }] }; createPlayerConfig(); changePlaylist(presentation, 1); }; </script> </code> html <code> <div id="side"> <br><br> <input type="button" class="plbtn" value="November 2010" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/Intro112210.flv', 'https://www.jala-mi.org/gmfiles/collections112210.flv')"> <br> <input type="button" class="plbtn" value="January 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="February 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/Intro22811.flv', 'https://www.jala-mi.org/gmfiles/jpd_3.flv')"> <br> <input type="button" class="plbtn" value="March 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/Intro32811.flv', 'https://www.jala-mi.org/gmfiles/assets32811.flv')"> <br> <input type="button" class="plbtn" value="April 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="May 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="September 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="October 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> <input type="button" class="plbtn" value="November 2011" style="background-color:#0D4A80; color:#fff" onClick="javascript:loadandplay('https://www.jala-mi.org/gmfiles/intronone.flv', 'https://www.jala-mi.org/gmfiles/')"> <br> </div> </code> Similar TutorialsI've created a web application which uses just JS\Jquery\ajax, without any page reloads. The issue is, it starts working really slow after using it for a while since it probably has lots of memory leaks. No i'm reading about memory consumption and memory leaks and my first question is about the following scenario. I have an ajax request each second which checks for new messages. and when i get the response, the following loop is part of the code. Code: for (i=0; i<pm_length; i++) { var content = data.pm[i].content; var mood = data.pm[i].mood; var time_added = data.pm[i].time_added; var fb_id = data.pm[i].user_id; var msg_id = data.pm[i].id; var user_status = data.pm[i].user_status; var full_name = data.pm[i].first_name + " " + data.pm[i].last_name; if($("#pm"+fb_id).length == 0) { var closetab = '<a class="close">x</a>'+full_name; $(".tabs").append('<li class="bot_room" id="pm'+fb_id+'" type="pm" tab="#tabpm'+fb_id+'">'+closetab+'</li>'); $(".tab_container").append('<div id="tabpm'+fb_id+'" class="tab_content" style="display: none;"></div>'); } create_message('pm'+fb_id, msg_id, fb_id, full_name, content, 'no', user_status, time_added, mood); if($("#pm_alert").attr('checked')) { soundManager.onready(function(){ var mySound = soundManager.createSound({ id:'mySound1', url:'images/new_msg.mp3' }); mySound.play(); mySound = null; }); } content = null; mood = null; time_added = null; fb_id = null; msg_id = null; user_status = null; full_name = null; } and my question is, is there a reason to actually null the variables if they will be redeclared any way on the following second? Hi every1! Got a task in university to make a memory game (card matching game). Thats what i've got so far: Code: var selected = 0; var choiceOne; var choiceTwo; var matches = 0; var numOfTries = 0; var interval = 700; var gamePlace = document.getElementById("wrap"); var backcard="img/memoryBg.png"; var timeStart; var timeFinish; var time; function nameEnter(){ var name = prompt("Please enter your name"); if (name != null){ document.title = "Welcome " + name + "!"; } } function game(){ var nOfPairs = document.getElementById("nPairs").value; timeStart = new Date().getTime(); if (nOfPairs==1){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; } if (nOfPairs==2){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==3){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==4){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==5){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==6){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards[10] = 'img/card6.png'; cards[11] = 'img/card6.png'; cards.sort(function shuffle() { return Math.random() - 0.5;}); } if (nOfPairs==7){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards[10] = 'img/card6.png'; cards[11] = 'img/card6.png'; cards[12] = 'img/card7.png'; cards[13] = 'img/card7.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if (nOfPairs==8){ var cards=[]; cards[0] = 'img/card1.png'; cards[1] = 'img/card1.png'; cards[2] = 'img/card2.png'; cards[3] = 'img/card2.png'; cards[4] = 'img/card3.png'; cards[5] = 'img/card3.png'; cards[6] = 'img/card4.png'; cards[7] = 'img/card4.png'; cards[8] = 'img/card5.png'; cards[9] = 'img/card5.png'; cards[10] = 'img/card6.png'; cards[11] = 'img/card6.png'; cards[12] = 'img/card7.png'; cards[13] = 'img/card7.png'; cards[14] = 'img/card8.png'; cards[15] = 'img/card8.png'; cards.sort(function shuffle(){ return Math.random() - 0.5;}); } if(nOfPairs>8||nOfPairs<1){ alert("Please enter the number from 1 to 8"); }else{ for(i = 0; i<=nOfPairs*2-1; i++){ var cardDiv = document.createElement("div"); cardDiv.setAttribute("id",""+i+""); cardDiv.style.height="65px"; cardDiv.style.width="65px"; cardDiv.style.float='left'; gamePlace.appendChild(cardDiv); document.getElementById(""+i+"").innerHTML='<img src="img/memoryBg.png">'; cardDiv.onclick=function(){cardClick(this.id);}; } } function cardClick(card){ if (selected==2){ return ; } if (selected==0){ choiceOne=card; document.images[card].src = cards[card]; selected = 1; }else { selected = 2; choiceTwo = card; document.images[card].src =cards[card]; var timer=setInterval(checking,interval); } function checking() { clearInterval(timer); numOfTries++; document.getElementById("tries").innerHTML = numOfTries; if (cards[choiceTwo]==cards[choiceOne]){ matches++; document.images[choiceOne].src = "img/memoryBgI.png"; document.images[choiceTwo].src = "img/memoryBgI.png"; //choiceTwo.onclick=null; } else { document.images[choiceOne].src = "img/memoryBg.png"; document.images[choiceTwo].src = "img/memoryBg.png"; selected = 0; return ; } if (matches == nOfPairs){ timeFinish = new Date().getTime(); time = Math.round (((timeFinish - timeStart)/1000)); alert("Congrats! You needed " + numOfTries + " tries, and " + time + " seconds."); } selected = 0; return ; } } } so the problem is, that I can just click 2 times on one card and it will look like a coincidence. My second problem is that when I have a real coincidence, I can still click on that card (by "real coincidence" i mean two same pics. so when i find them, they have to be not active, and when im clicking on them, they should just simply do nothing.). Can someone help me? Okay, so I'm making something so IF the number is BETWEEN this number, and that number, I want it to display a certain number. Ex. Code: var Num1 = 100 var Num2 = 200 var Num3 = 1 If (Num1{THIS IS WHAT I DON'T KNOW}Num2) { alert(Num3) } Also, could somebody please tell me what those are called? The; == != || etc. Because I was having a TERRIBLE time trying to google that. AHH! I'm sorry for this. I know this is a stupid question. Hi everyone, Really hoping someone can help me with a javascript / memory problem I'm having. Environment is Win7, MSIE v8.0. I have a pretty simple test function for this problem: Code: function go() { // create giant text string in variable 'bill' then set 'bill' to null - store inside variable called 'test' as text. var test = "bill = 'blah blah blah blah blah (repeat blah about a thousand times or so for file size)'; bill = null;"; // execute text inside variable 'test' as code var tester = new Function(test); // destroy function used to execute string as code tester = null; } now, when you trigger the function go() through a button (ie <button onclick='go();'>go</button>) you can watch the memory usage in taskmgr go up every time you click the button (for me it goes up about 3MB every time go() is called - but that will depend on how many blah's you have). I have tried replacing var tester = new Function(test); with the eval() command... Same problem. It would appear the function is creating (storing in memory) a new instance every time its called ... What am I doing wrong here?? How do I stop the memory increasing every time i try to execute the string 'test' as script? Checked this code in Chrome and Firefox - they don't seem to have any memory issues running this code - its only a IE issue (*sigh*). That doesnt help me as the problem relates to a client's HTA application (can't switch off IE engine). Huge thanks in advance! Doing some RSA encryiption in java script Nothing big, strings like max 20 chars in lenght. After a couple (random, could be 10 or 50 or anything operations) of that, browser does not freeze, server does not freeze but wont work anymore on any string, i.e same result for any string. Restarting browser helps. What should I look for, are there any tools for FF to look for js memory leaks an such ? I am using firefox 8. Is there a way is seeing the details of its memory usage please? Many thanks for any help, S Greeting....! Does anyone know that how's the JavaScript memory model work? I mean as we know that a Stack stores primitive type variables and references, a Heap stores referenced type object, what about functions? Should functions are stored at a Memory or what I said is wrong. Is there any books or resources are talking about JavaScript Memory Model? Thanks a lot!!!! Dan ok, i'm sure you all will laugh at this, but it seems really bizzario to me. i have two small routines worked out. routine G gets a value out of the db and brings it back to the browser script. routine P puts a value into the db from the browser script. they both run fine. but......... you have to turn the browser totally off and restart it after retrieving a value out of the db (?) why? is this some kind of "clear memory" i need to be doing. its like the last value i retrieved thru the server is stuck in memory and keeps coming up, and it won't flush out until i turn the browser off and restart it. is this a Javascript problem or a PHP problem? i dont' have a clue, maybe i need to somehow flush the memory in the PHP file after i echo() back the result.? is that normal? thanks, Paul I'm working on a Virtual Tabletop project and am running into issues where tons of memory is accumulated. I've set up a test where 502 pieces are on the board and every second the board randomly zooms in/out 5%. After 30 seconds, I clear the interval. http://ontodevelopment.com/boardtest/ When the board zooms, every game piece has has to be re-sized. Using Firefox, watch the memory go up but never come back down. In IE and Chrome the leak is no where near as bad. Any ideas or pointers would be appreciated. I'm designing a page to fade multiple images (three) in and out over a set period of time (one minute, say). The code I have works as follows: I define an array and fill it with the div objects I want to fade in and out. I have some simple tags (2 for opaque, 1 for fading in, -1 for fading out, etc.) that I set to show the state of the object. First I check if the object is opaque, and if so set it to fade along with a time; Or if it's transparent I give it a 1 in 10 chance of fading in during this iteration. If it's currently fading in or out I check to make sure it's on track and change the opacity of the div accordingly. Originally, I had the function call back to itself using setTimeout after the loop had gone through all the divs and changed their opacities appropriately. This caused an out of memory error (though, strangely, if I had any alerts in the function it didn't give me the error). So the version below has the code in a while loop to let it run for 20 seconds - this gives me the "a script...run slowly, do you want to continue the script?" message. So I have several questions: 1) First and foremost, why is my function causing out of memory/run slowly errors and what is the best way to fix that? 2) Is there a better way to code this effect (final result will be six lights fading in and out for a minute or so). 3)I'm not an experienced programmer, so any bad practices, ways to streamline, no no's, etc. that you see, please point out. I've only posted the script here - the page simple consists of three divs (id's grad0, grad1, grad2) and a call to the function copied here. Thanks in advance. Code: function commencez(){ //parent function to store var's var TimeToFade = 10000; theArray = new Array(2); var wrench = new Date().getTime(); for (var a=0; a<3; a++){ //fill the array with objects theArray[a] = document.getElementById("grad" + a); theArray[a].FadeState = -2; //start them transparent } function fader() { var check = new Date().getTime(); //stops the function after 30 seconds if( check <= wrench + 30000 ) { return; } for(var i=0; i<3; i++){ if (theArray[i].FadeState !=1 && theArray[i].FadeState != -1) { if( theArray[i].FadeState == 2) { theArray[i].FadeState = -1; theArray[i].FadeTimeLeft = TimeToFade; theArray[i].FadeBegan = new Date().getTime(); } if( theArray[i].FadeState == -2) { var rndm=Math.floor(Math.random()*11); if( rndm == 1) { theArray[i].FadeState = 1; theArray[i].FadeTimeLeft = TimeToFade; theArray[i].FadeBegan = new Date().getTime(); } } } if( theArray[i].FadeState == 1 || theArray[i].FadeState == -1) { var now = new Date().getTime(); since = now - theArray[i].FadeBegan; if( theArray[i].FadeTimeLeft <= since ) { theArray[i].style.opacity = theArray[i].FadeState == 1 ? '1' : '0.2'; theArray[i].style.filter = 'alpha(opacity = ' + (theArray[i].FadeState == 1 ? '100' : '20') + ')'; theArray[i].FadeState = theArray[i].FadeState == 1 ? 2 : 2; } theArray[i].FadeTimeLeft -= since; var howMuch = theArray[i].FadeTimeLeft / TimeToFade; if( theArray[i].FadeState == 1) { howMuch = 1-howMuch; theArray[i].style.opacity = howMuch; theArray[i].style.filter = 'alpha(opacity = ' + (howMuch*100) + ')'; } if( theArray[i].FadeState == -1) { theArray[i].style.opacity = howMuch; theArray[i].style.filter = 'alpha(opacity = ' + (howMuch*100) + ')'; } } } //close the for loop setTimeout( fader() , 500); //last thing fader does is recall itself after half a second } //close the fader function fader(); } //close parent function </script> I have been beating my head against the wall trying to figure this out for hours. It seems like it should be so easy, but here is my code: Code: var tempMyPlayersArray = document.getElementsByClassName("player"); var myPlayersArray = new Array(); for (var p=0; p<playerColumnTeamArray.length; p++) { var stripPlayerColumnElement = playerColumnTeamArray[p].innerHTML.stripTags(); var pMatches = stripPlayerColumnElement.match(/(.*?) \((.*?) - (.*?)\)/); if(pMatches) myPlayersArray.push(pMatches[1]); } //This yields "Tony Romo" GM_log(myPlayersArray[0]); //This yields "false". GM_log((myPlayersArray[0]) == 'Tony Romo'); Note that stripTags() removes all of the html tags and leaves you with the folowing (or similar) for every element: Tony Romo (Dal - QB) So my overall question is why that last GM_log statement in the code is yielding false. I have made sure that the elements are all strings (using toString()), checked for white space issues (using repace()), and made sure the regex is working okay. No matter what though, I get false even though "Tony Romo" is clearly the only string in myPlayersArray[0]. Thanks! Hi all, I am mixing googles geolocation with it's local search API. This sounds complicated but I think my problem is primarily one involving passing a value to an array. Here is my code Code: <script type="text/javascript"> var lat,lon; var scrnText; function main() { scrnText = document.getElementById('message'); if (navigator.geolocation) { scrnText.innerHTML = "acquiring your position..."; navigator.geolocation.getCurrentPosition(reportLatLon, function(error) { scrnText.innerHTML = "Couldn't acquire location (error "+error.code+"). "; }); } else { scrnText.innerHTML = "geolocation API not available!"; } } function reportLatLon(pos) { lat = pos.coords.latitude; lon = pos.coords.longitude; accuracy = Math.round(pos.coords.accuracy); scrnText.innerHTML="your location is " + lat + ", " + lon + " to "+ accuracy + " metres accuracy"; } </script> <script src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" type="text/javascript"></script> <script type="text/javascript"> google.load('search', '1'); var localSearch; function searchComplete() { document.getElementById('content').innerHTML = ''; if (localSearch.results && localSearch.results.length > 0) { for (var i = 0; i < localSearch.results.length; i++) { var p = document.createElement('p'); var a = document.createElement('a'); var b = document.createElement('b'); var c = document.createElement('c'); a.href = localSearch.results[i].url; a.innerHTML = localSearch.results[i].title; b.innerHTML = "<br>" + localSearch.results[i].streetAddress; c.innerHTML = "<br>" + localSearch.results[i].city + "," + localSearch.results[i].region; p.appendChild(a); p.appendChild(b); p.appendChild(c); document.body.appendChild(p); } } } function onLoad() { localSearch = new google.search.LocalSearch(); var lat,lng,str navigator.geolocation.getCurrentPosition(handler); function handler(position) { lat = pos.coords.latitude; lng = pos.coords.longitude; str = lat + "," + lng; localSearch.setCenterPoint(str); } localSearch.setSearchCompleteCallback(this, searchComplete, null); localSearch.execute('cinema'); google.search.Search.getBranding('branding'); } google.setOnLoadCallback(onLoad); </script> </head> <!-- when this html page is loaded by a browser, it calls the 'main' javascript function --> <body onload="main()"> <h1>Geolocation Demo</h1> <h2>1. GeoLocation API</h1> <!-- this is where the html object updates text displayed to the user --> <span id="message"></span> <div id="branding" style="float: left;"></div><br /> <div id="content"></div> </body> All I want to do is pass the previous "lat,lon" value from the geolocation, into the "localSearch.setCentrepint part. As you will see I have tried using the following Code: Code: var lat,lng,str navigator.geolocation.getCurrentPosition(handler); function handler(position) { lat = pos.coords.latitude; lng = pos.coords.longitude; str = lat + "," + lng; localSearch.setCenterPoint(str); } but this will not work. I am a php coder so let me apologise in advance if i have worded this confusingly. Thanks in advance Dan Hi, New member, not really well versed in JS, and need help with the following. What is supposed to happen is the script will take today's day and then check the array to see if the date falls within a certain range of dates, and then return the appropriate items. Could someone please have a look at the code and see why it isn't working. Thanks in advance. George BTW; This is a stripped down version. Code: <script > // For months 0 = January var ch_y_S = "2010"; //X-mass season start; year var ch_m_S = "11"; //X-mass season start; month var ch_d_S = "25"; //X-mass season start; day var ch_y_E = "2011"; //X-mass season end; year var ch_m_E = "0"; //X-mass season end; month var ch_d_E = "9"; //X-mass season end; day var ot1_y_S = "2011"; var ot1_m_S = "0"; var ot1_d_S = "10"; var ot1_y_E = "2011"; var ot1_m_E = "2"; var ot1_d_E = "8"; var le_y_S = "2011"; var le_m_S = "2"; var le_d_S = "9"; var le_y_E = "2011"; var le_m_E = "3"; var le_d_E = "23"; var ad = "Advent"; var ch = "Christmas"; var ot = "Ordinary Time"; var le_ = "Lent"; var eas = "Easter"; var purple = PURPLE; var white = WHITE; var green = GREEN; var today = new Date(); var a = new Array ( { start: new Date(ch_y_S,ch_m_S,ch_d_S), end: new Date(ch_y_E,ch_m_E,ch_d_E,23,59,59), season: ch, txtColor:whi }, { start: new Date(ot1_y_S,ot1_m_S,ot1_d_S), end: new Date(ot1_y_E,ot1_m_E,ot1_d_E,23,59,59), season: ot, txtColor:gre }, { start: new Date(le_y_S, le_m_S,le_d_S), end: new Date(le_y_E, le_m_E,le_d_E,23,59,59), season: le_, txtColor:pur } ); var whatz = " "; var whatz1 = " "; for (i = 0; i < a.length; i++) { if ((today >= a[i].start) && (today <= a[i].end)) { whatz = a[i].color; whatz1 = a[i].season; } } document.write(whatz ) document.write('<br />' ) document.write(whatz1 ) </script I have two arrays that are tied together Ink1Data[][InkID] and Ink1Data[][Ink1Desc] I also have this Javascript code to validate it: Code: var chksink1 = document.getElementsByName("Ink1Data[][InkID]"); for (var i = 0; i < chksink1.length; i++) { switch (chksink1[i]) { case (chksink1.value=1); var i1Desc = "Ink1Data["+i+"][Ink1Desc]"; if (validate_required(i1Desc,"Please fill in a trim size.")==false) {i1Desc.focus();return false;} break; case (chksink1.value=2); var i1Desc = "Ink1Data["+i+"][Ink1Desc]"; if (validate_required(i1Desc,"Please fill in folding instructions.")==false) {i1Desc.focus();return false;} break; case (chksink1.value=3); var i1Desc = "Ink1Data["+i+"][Ink1Desc]"; if (validate_required(i1Desc,"Please fill in scoring instructions.")==false) {i1Desc.focus();return false;} break; } } basically, I want to count how many are in the array (will be the same number for both) and then depending on the value of the first array, validate the second and kick an appropriate error if necessary. it should be pretty easy, but I can't seem to get the code to work. Is there anyone that could please help me figure this out? Thank you! Hey all, first, if I missed this in my search, I apologize. Now on to the problem at hand. I'm reading an xml file of html links into a webpage I'm building on the intranet. I have assigned each of a particular element of that xml file to a specific location within a javascript array. Then I built a second javascript array and assigned the value of the first array to second array using the .toString(); method. Here's an example of what I've got so far: Code: var linkArray = new Array(); linkArray[0] = (xmlDoc.getElementsByTagName("link")[0].childNodes[0].nodeValue); linkArray[1] = (xmlDoc.getElementsByTagName("link")[1].childNodes[0].nodeValue); linkArray[2] = (xmlDoc.getElementsByTagName("link")[2].childNodes[0].nodeValue); linkArray[3] = (xmlDoc.getElementsByTagName("link")[3].childNodes[0].nodeValue); linkArray[4] = (xmlDoc.getElementsByTagName("link")[4].childNodes[0].nodeValue); linkArray[5] = (xmlDoc.getElementsByTagName("link")[5].childNodes[0].nodeValue); linkArray[6] = (xmlDoc.getElementsByTagName("link")[6].childNodes[0].nodeValue); linkArray[7] = (xmlDoc.getElementsByTagName("link")[7].childNodes[0].nodeValue); var pageArray = new Array(); pageArray[0] = linkArray[0].toString(); pageArray[1] = linkArray[1].toString(); pageArray[2] = linkArray[2].toString(); pageArray[3] = linkArray[3].toString(); pageArray[4] = linkArray[4].toString(); pageArray[5] = linkArray[5].toString(); pageArray[6] = linkArray[6].toString(); pageArray[7] = linkArray[7].toString(); I have put documentDOTwriteln statements in at various places to test the values of both arrays to be sure they are the same and they are. Now, later in the code, I'm loading an iframe that targets a specific element of the second array for the page link. It does this through a variable called currentPage thus: theIframe.src = pageArray[currentPage]; where currentPage is previously assigned a value pointing to an element in the array. For example currentPage = 0;. I'm trying to replace the original code which looks like this and works: Code: var pageArray = new Array(8); pageArray[0] = "html/chapters/chapter1_Introduction/chapter1.html"; pageArray[1] = "html/chapters/chapter2_Change-Location/chapter2.html"; pageArray[2] = "html/chapters/chapter3_Change-Manager/chapter3.html"; pageArray[3] = "html/chapters/chapter4_Change-Salary/chapter4.html"; pageArray[4] = "html/chapters/chapter5_Change-Assignment/chapter5.html"; pageArray[5] = "html/chapters/chapter6_Termination/chapter6.html"; pageArray[6] = "html/chapters/chapter7_Request-Access-to-MSS/chapter7.html"; pageArray[7] = "html/chapters/chapter8_Summary/chapter8.html"; with the contents of the xml file so I can load different pages into a template. I don't understand what I'm doing wrong. Can anyone point me in the right direction? I believe my xml is parsing correctly since I can do a documentDOTwriteln and see the contents when placed into an array. Am I wrong on this? Any and all help you can give is greatly appreciated! M Hey guys I got a problem with my program, it converts a name to initial example: Jordy Derp ==> J.D. but i always get an undefined in front of it http://i43.tinypic.com/2qu12jn.png Code: addEventListener("load", init, false); function init() { var naam = new initialen(prompt("geef een naam in")); var tijdelijk = "" var naamArray = new Array(); var perm = "" function initialen(naam) { if (naam.indexOf(' ') >= 0) { naamArray = naam.split(" ") for (var i = 0; i < naamArray.length; i++) { perm += naamArray[i].charAt(0) + "." } alert(perm) } else { alert("False") } } } btw it also checks for spaces so u need to type 2 names Thanks in advance Hi, I'm new to javascript and have a small but annoying problem with an array. The script rotates a randomly selected banner from an array and changes the banner every few seconds. The problem is that when the page is first loaded the word 'undefined' displays next to the first banner. When the second banner is displayed everything works fine. Here is the function I am using Code: function randomBanner() { var myBanners=new Array(3); var i = Math.floor(Math.random() * 3); myBanners[0]="image 1"; myBanners[1]="image 2"; myBanners[2]="image 3"; document.getElementById('banners').innerHTML = myBanners[i]; setTimeout("randomBanner()",5000 ); } and here is the html Code: <head> <script type="text/javascript" src="rotator.js"></script> </head> <body> <div id="banners"> <script type="text/javascript">document.write(randomBanner());</script> </div> </body> I have tried declaring the variables and even the array itself outside the function to no avail. I have set up a test page so you can view the problem here Please can someone help? Hi.. I have problem on how can I automatically display the output from the computation (Demanded Qty * Quantity), after or while I input Demanded Qty. I mean I input Demanded Qty, on below it will display the output also beside the SubItems. Sorry, if i post again my problem, because I have no idea on how can I solve this problem. here is my code: PHP Code: <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <html> <title>Stock Requisition</title> <head> <link rel="stylesheet" type="text/css" href="kanban.css"> <script type="text/javascript"> function compute_quantity(){ var DemandedQty = document.getElementById('DemandedQty').value; var SubQty = document.getElementById('SubQty').value; var Quantity = document.getElementById('Quantity').value; SubQty = (DemandedQty * Quantity); } </script> </head> <body> <form name="stock_requisition" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div> <table> <thead> <th>Items</th> <th>Sub Items</th> <th>Item Code</th> <th>Demanded Qty</th> <th>UoM</th> <th>Class</th> <th>Description</th> <th>BIN Location</th> </thead> <?php $DemandedQty = $_POST['DemandedQty']; $sql = "SELECT DISTINCT Items FROM bom_subitems ORDER BY Items"; $res_bom = mysql_query($sql, $con); while($row = mysql_fetch_assoc($res_bom)){ $Items = $row['Items']; $Items_ = substr($Items, 12, 3); echo "<tr> <td style='border: none;font-weight: bold;'> <input type='name' value='$Items_' name='Items_[]' id='Items' readonly = 'readonly' style = 'border:none;width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border:none;'> </td> <td style='border:none;'> </td> <td style='border: none;'><center><input type='text' name='DemandedQty[]' id='DemandedQty' value='' size='12' onkeyup = compute_quantity()></center></td> </tr>"; $sql = "SELECT Items, SubItems, ItemCode, UoM, Class, Description, BINLocation, Quantity FROM bom_subitems WHERE Items = '$Items' ORDER BY Items"or die(mysql_error()); $res_sub = mysql_query($sql, $con); while($row_sub = mysql_fetch_assoc($res_sub)){ $Items1 = $row_sub['Items']; $SubItems = $row_sub['SubItems']; $ItemCode = $row_sub['ItemCode']; $UoM = $row_sub['UoM']; $Class = $row_sub['Class']; $Description = $row_sub['Description']; $BINLocation = $row_sub['BINLocation']; $Quantity = $row_sub['Quantity']; echo "<input type='hidden' name='Quantity' id='Quantity' value='$Quantity'>"; echo "<tr> <td style='border: none;'> <input type='hidden' value='$Items1' id='Items1' name='Items1[]'></td> <td style='border: none;'> <input type='text' name='SubItems[]' value='$SubItems' id='SubItems' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'> <input type='text' name='ItemCode[]' value='$ItemCode' id='ItemCode' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'><center><input type='text' name='SubQty[]' id='SubQty' value='' size='12' style></center></td> <td style='border: none;' size='3'> <input type='text' name='UoM[]' value='$UoM' id='UoM' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size='3'></td> <td style='border: none;'> <input type='text' name='Class[]' value='$Class' id='Class' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> <td style='border: none;'> <input type='text' name='Description[]' value='$Description' id='Description' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;' size= '30'></td> <td style='border: none;'> <input type='text' name='BINLocation[]' value='$BINLocation' id='BINLocation' readonly='readonly' style='border:none; width:auto;font-family: Arial, Helvetica, sans-serif;font-size: 1em;'></td> </tr>"; } } ?> </table> </div> </form> </body> </html> If you don't understand my issue feel free to ask me for further understanding. I'm sorry again I really need to solve this and i tried to google but I can't find the solution. Thank you so much Hello, I've a checkbox validation script to check for at least one ckeck. Code: <script type="text/javascript"> function chkChecks(){ isChecked=false alert(document.forms["form1"]["checked[]"].length); for(var i=0;i<document.forms["form1"]["checked[]"].length;i++){ if(document.forms["form1"]["checked[]"][i].checked){ isChecked=true } } if(isChecked){ document.forms["form1"].submit() } else{ alert('Please select a checkbox') } } </script> for the form Code: <form name="form1" action="test.php"> <input type="checkbox" name="checked[]" value="1"> <input type="checkbox" name="checked[]" value="2"> <input type="checkbox" name="checked[]" value="3"> <input class="button_140" type="button" value="Check" onclick="chkChecks()"> </form> It works well for more than one checkboxes, but these are generated dynamically by PHP and if there is only one checkbox, document.forms["form1"]["checked[]"].length returns 'undefined'. Can I modify this to work in all cases? thanks, art. Code: var rollnav = []; rollnav[0] = new Image ; rollnav[0].src ="buttons/button 1 suits.png"; rollnav[1] = new Image; rollnav[1].src = "buttons/button 2 suits.png"; rollnav[2] = new Image; rollnav[2].src ="buttons/button 1 dresses.png"; rollnav[3] = new Image; rollnav[3].src ="buttons/button 2 dresses.png"; rollnav[4] = new Image; rollnav[4].src ="buttons/button 1 christening.png"; rollnav[5] = new Image; rollnav[5].src ="buttons/button 2 christening"; rollnav[6] = new Image; rollnav[6].src ="buttons/button 1 Acessories.png"; rollnav[7] = new Image; rollnav[7].src ="buttons/button 2 Acessories.png"; rollnav[8] = new Image; rollnav[8].src ="buttons/button 1 home.png"; rollnav[9] = new Image; rollnav[9].src ="buttons/button 2 home.png"; rollnav[10] = new Image; rollnav[10].src ="buttons/button 1 contact us.png"; rollnav[11] = new Image; rollnav[11].src ="buttons/button 2 contact us.png"; rollnav[12] = new Image; rollnav[12].src ="buttons/button 1 links.png"; rollnav[13] = new Image; rollnav[13].src ="buttons/button 2 links.png"; function SwapOutsuit(){ document.suitnav.src = rollnav[1].src; return true; } function SwapBacksuit(){ document.suitnav.src = rollnav[0].src; return true; } function SwapOutdress(){ document.dressnav.src = rollnav[3].src; return true; } function SwapBackdress(){ document.dressnav.src = rollnav[2].src; return true; } function SwapOutchris(){ document.chrisnav.src = rollnav[5].src; return true; } function SwapBackchris(){ document.chrisnav.src = rollnav[4].src; return true; } function SwapOutaccess(){ document.accessnav.src = rollnav[7].src; return true; } function SwapBackaccess(){ document.accessnav.src = rollnav[6].src; return true; } function SwapOuthome(){ document.homenav.src = rollnav[9].src; return true; } function SwapBackhome(){ document.homenav.src = rollnav[8].src; return true; } function SwapOutcus(){ document.cusnav.src = rollnav[11].src; return true; } function SwapBackcus(){ document.cusnav.src = rollnav[10].src; return true; } function SwapOutlink(){ document.linknav.src = rollnav[13].src; return true; } function SwapBacklink(){ document.linknav.src = rollnav[12].src; return true; } hi im trying to create a series of rollover buttons for my site that can be used on all pages. im using the above array to switch the button images the problem is they all work up until number 9 then they just display the image set as the original image and dont change on mouse over. i try declaring an array with more than 9 spaces it doesn't seem to make a difference ive tried the new array format to with no avail. any help would be greatly appreciated. i have included my code which is embedded in my html to call the function below. Code: <A href="coolkids formal contact us.html" onMouseOver="SwapOutcus()" onMouseOut="SwapBackcus()"><img src="buttons/button 1 contact us.png" name="cusnav" align="left" ></A> <A href="information.html" onMouseOver="SwapOutlink()" onMouseOut="SwapBacklink()"><img src="buttons/button 1 links.png" name="linknav" align="left" ></A> |