JavaScript - Error Checking
I made a cool calendar application I want someone to look over it and see if I messed up anywhere cause I have a good feeling I did. It was designed to be an active desktop ^.^ that way I get my desktop image and a calendar ^.^
index.html Code: <html> <head> <title>Desktop</title> <link rel="stylesheet" type="text/css" href="css/mainTemplate.css" /> </head> <body> <img id="bgImage" src="images/nerbian-entrance.jpg" alt="Active Desktop made by Shelby Brittain" /> <table id="mainTable"> <caption id="caption"> </caption> <tr> <th> Sunday </th> <th> Monday </th> <th> Tuesday </th> <th> Wednesday </th> <th> Thursday </th> <th> Friday </th> <th> Saturday </th> </tr> <tr> <td id="d1"> </td> <td id="d2"> </td> <td id="d3"> </td> <td id="d4"> </td> <td id="d5"> </td> <td id="d6"> </td> <td id="d7"> </td> </tr> <tr> <td id="d8"> </td> <td id="d9"> </td> <td id="d10"> </td> <td id="d11"> </td> <td id="d12"> </td> <td id="d13"> </td> <td id="d14"> </td> </tr> <tr> <td id="d15"> </td> <td id="d16"> </td> <td id="d17"> </td> <td id="d18"> </td> <td id="d19"> </td> <td id="d20"> </td> <td id="d21"> </td> </tr> <tr> <td id="d22"> </td> <td id="d23"> </td> <td id="d24"> </td> <td id="d25"> </td> <td id="d26"> </td> <td id="d27"> </td> <td id="d28"> </td> </tr> <tr> <td id="d29"> </td> <td id="d30"> </td> <td id="d31"> </td> <td id="d32"> </td> <td id="d33"> </td> <td id="d34"> </td> <td id="d35"> </td> </tr> <tr> <td id="d36"> </td> <td id="d37"> </td> <td id="d38"> </td> <td id="d39"> </td> <td id="d40"> </td> <td id="d41"> </td> <td id="d42"> </td> </tr> </table> </div> <script type="text/javascript" src="js/mainLib.js"> //<![CDATA[ //]]> </script> </body> </html> css/mainTemplate.css Code: body { background-color: black; color: #C0C0FF; } caption { font-size: 24pt; } th, td { width: 100px; border: 1px solid #C0C0FF; } th { height: 25px; } td { vertical-align: bottom; text-align: right; } #bgImage { position: absolute; top: 0px; left: 0px; width: 102%; height: 103%; } #mainTable { position: absolute; top: 2%; left: 61%; border: 1px solid #C0C0FF; padding: 5px; vertical-align: top; width: 600px; height: 375px; background-color: black; opacity:0.5; filter:alpha(opacity=50); } js/mainLib.js Code: var d = new Date(); var day = d.getDate(); var wk = d.getDay(); var set = new Array(35); var j = 0; var mnth = d.getMonth(); function getMonthName() { var mth = ""; var month = new Array(12); month[0]="January"; month[1]="Febuary"; month[2]="March"; month[3]="April"; month[4]="May"; month[5]="June"; month[6]="July"; month[7]="August"; month[8]="September"; month[9]="October"; month[10]="November"; month[11]="December"; mth = month[d.getMonth()]; return mth; } function getFirstDay() { var firstDay = wk; for(var i=day-1; i>=0; i--) { firstDay--; if(firstDay == -1) { firstDay=6; } } return firstDay; } if(mnth==1 && getFirstDay()!=6) { monthLength = 28; } else if(mnth==1 && getFirstDay()==6) { monthLength = 28+6; } else if(mnth==0 || mnth==3 || mnth==5 || mnth==8 || mnth==10) { monthLength = 30+getFirstDay(); } else { monthLength = 31+getFirstDay(); } document.getElementById("caption").innerHTML = getMonthName(); if(getFirstDay()==6) { for(var i=1; i<=monthLength-6; i++) { j++; set[i] = "d"+ (i); document.getElementById(set[i]).innerHTML = j; } } else { for(var i=getFirstDay()+1; i<=monthLength; i++) { j++; set[i] = "d"+ (i + 1); document.getElementById(set[i]).innerHTML = j; } } Thanks Shelby Similar TutorialsHi all, I have this code in javascript that is basically a number base converter and it works fine, all I want to do is add error checking, and I think it would probably be done with if statements?? But I'm not sure where they should go or how to write one, but I know all it will need is basically to say all numbers from 0-9 and all letters from A-F are ok and for the letters to be case insensitive, does that need an "i" flag or something? Anything else which someone tries to put in I want an alert to appear to say like invalid input or something, can someone help please?? Here is my code: Code: </script> <table cellpadding=5px> <tr> <td> Input Number: <textarea rows="2" name="inbox" cols="20" wrap="virtual"></textarea><br> </td> <td> Input base: <select name=from> <option name= dec value=10>Decimal</option> <option name= bin value=2>Binary</option> <option name= hex value=16>Hexidecimal</option> <option name= oct value=8>Octal</option> </select> </td> </tr> <tr> <td> Output Number: <textarea rows="2" name="outbox" cols="20" wrap="virtual"></textarea><br> </td> <td> <br> Output base <select name=to> <option name= bin value=2>Binary</option> <option name= hex value=16>Hexidecimal</option> <option name= oct value=8>Octal</option> <option name= dec value=10>Decimal</option> </select> </td> </tr> <tr> <td> <input type="button" value="Convert" onclick="javascript:convert();"> </td> </tr> </table> </form> Thanks to all who help hello, This seems very basic, but I am running into a complication. What I would like to do is check if a certain div is displayed or hidden (really doesnt matter which) then set a map zoom level based upon that. here is my current function. function closeDirections(){ gdir.clear(); if(document.getElementById("river").style.display="none"){ map.setZoom(16); hideID('river'); showID('main'); } if(document.getElementById("main").style.display="none"){ map.setZoom(12); hideID('main'); showID('river'); } } Also here is a link to my map, just in case. map I have tried several variations, but from what I can tell with every instance, it only examines the first and never considers the second "if". I thought to rememdy this issue by using if/else...same issue. The only thing I could think is a nested if statements, but I am not sure what to use as a parent if. I have searched for a while, but cannot seem to find something like an .is_hidden(). Something that I could see if its true/false. Any help would be very much appreciated. I keep getting myself into trouble because I tend to use my Java reference when coding Javascript, and of course Javascript doesn't support all Java functions. I've just discovered that apparently Javascript doesn't support the trim() function. But curiously, if I code it as: mystring.trim() then Firefox will handle it without any errors. (I haven't actually checked to see if whitespace is removed from around the string.) But when I code it like this: trim(mystring) then firefox gives an error. I've also found a couple of situations where I've made syntax errors, and Firefox gives no error message. This makes debugging tricky, since the only resource I have available is Firefox's Error Console. Safari has no error reporting whatsoever, AFAIK. MSIE seems to find some errors that Firefox misses, but I don't have MSIE on my main computer, so it's a nuisance to use. As a hobbyist, I have no budget to buy Javascript development tools. Is there something free available that does a better job of finding errors? I need to check to see if a cookie has been previously created: Code: var startnum = readCookie('a1'); if (document.cookie.indexOf('a1') == -1){ var startnum = "0"; } Should line 2 be: Code: if (document.cookie.indexOf('a1') == null){ ? Does == -1 do the same thing? Hi, I know this is painfully obvious, but I can't get my head around it. Basically, these loops: Code: for (var p=0; p < routeInfo.length; p++) { times = routeInfo[p].getElementsByTagName("time"); dirs = routeInfo[p].getElementsByTagName("dirs"); dist = routeInfo[p].getElementsByTagName("dist"); for (q=0;q<dirs.length;q++) { count++; time = GXml.value(routeInfo[p].getElementsByTagName("time")[q]); dir = GXml.value(routeInfo[p].getElementsByTagName("dirs")[q]); dist = GXml.value(routeInfo[p].getElementsByTagName("dist")[q]); if (time.length>1) { way+='<div class="dir">'+count+'. '+dir+'</div>'+'<div class="results"><div class="time">'+time+'</div><div class="dist">'+dist+'</div><br><br>'; } else way+='<div class="dir">'+count+'. '+dir+'</div><br>'; } } outputs lines of text, with two <br> between them. What I want is for if it's the last loop for that only to be one <br> so I'm thinking something like this: Code: if (last loop) {end =<br>} else end=<br><br> ... way+='<div class="dir">'+count+'. '+dir+'</div>'+'<div class="results"><div class="time">'+time+'</div><div class="dist">'+dist+'</div>'+end; but I can't figure out what that "last loop" code should be. Any ideas? Hi. I'm having some trouble with validating all my fields in my form. I'm posting my code here. Code: function validateForm() {var chk = document.forms["EntryPage"]["studentname"].value; if (chk==null || chksdtnm=="") { alert("Student name must be filled out"); return false; } if (chk==null || chksdtnm=="") { alert("Student ID must be filled out"); return false; } } I need to alert the user with a messagebox for six fields if ever they're not filled in. How do I achieve that? _________
Hi all. This is a generic question as Google didn't help at all, does anyone know how to check if javascript is enabled and then I want to display info based on that. I don't mind how I do it, HTML, PHP or whatever Regards, Magnetica Hello all, i'm looking for a way to check if the name of the page is equal to a link inside the page being checked. for instance: inside the address bar: http://www.mysite.test.html <body> <ul> <li><a href="test.html"></a></li> <li><a href="test1.html"></a></li> <li><a href="test2.html"></a></li> <li><a href="test3.html"></a></li> </body> please advise. thanks, jav Hello all I'm trying to get this function to check for 10 digits only but alert shows regardless of less / more or exactly 10 digits --- what am I missing? any help on sorting this greatly appreciated. Code: function validate(form) { if (document.forms[0].callme.checked) { var phone = document.forms[0].mobile.value; // assign value to var phone = phone.replace( /[^\d]/g, "" ); // zap all non-digit characters if ( document.forms[0].mobile.length != 10 ) // chk for 10 digits{ alert("Enter 10 numbers"); return false; } else { alert("Yahoo"); } } low tech Hey guys. I need a little help with checking elements in my array. I created an array of size 50 and initialized all elements to 1. What i want to do is if the user types in the number "1" into the prompt box, i want it to check the first 15 elements and increase only one of the elements by 1, and if the element has already been increased by 1 then i want it to check the next element and see if its been increased, if the element has been increased then it needs to move on to the next one and check that. If the element is 1, then it can be increased by "1", if the element is 2 then i need it to check the next element. If all elements within the first 15 have increased by 1, it should just alert the user that all slots have been assigned. Is there a way to do this? Thanks for your time
I am using the following script on an e-commerce site to ensure that not both drop downs cannot be selected at the same time, as well as ensuring one must be selected at all times. Code: <script language="javascript"><!-- function validate(cart_quantity) { if (((cart_quantity.elements['id[2]'].value == 9999) && (cart_quantity.elements['id[3]'].value == 9999)) || ((cart_quantity.elements['id[2]'].value != 9999) && (cart_quantity.elements['id[3]'].value != 9999))) { alert('Please select only one option!'); cart_quantity.elements['id[2]'].value = 9999; cart_quantity.elements['id[3]'].value = 9999; cart_quantity.elements['id[2]'].focus(); return false; } } //--></script> The initial value of the drop down is set to 9999 off the bat. The problem with this code is that not all of my product pages have these 2 drop downs so I wanted to try and add in an if statement saying that if id2=9999 and id3 == null then pop up the error. That didnt work because I don't think it can be equal to null if it does not exist on the page. So instead I tried to do the following: Code: <script language="javascript"><!-- function validate(cart_quantity) { cart_quantity.elements['id[3]'] = 99999; if (((cart_quantity.elements['id[2]'].value == 9999) && (cart_quantity.elements['id[3]'].value == 9999)) || ((cart_quantity.elements['id[2]'].value != 9999) && (cart_quantity.elements['id[3]'].value != 9999))) { alert('Please select only one option!'); cart_quantity.elements['id[2]'].value = 9999; cart_quantity.elements['id[3]'].value = 9999; cart_quantity.elements['id[2]'].focus(); return false; } if (((cart_quantity.elements['id[2]'].value == 9999) && (cart_quantity.elements['id[3]'].value == 99999))) alert('Please select an option!'); cart_quantity.elements['id[2]'].value = 9999; return false; } //--></script> I tried to assign id3 a value of 99999 so that on pages that it doesnt get reassigned after (i.e pages it does not exist on) it will still be equal to 99999 and pop up the error according to the second if clause. Unfortunately that did not work either. Is there any other way I can do via javascript? Hey Guys, I am a student doing my Cert IV in IT and am having a bit of trouble with some code. I have a form that when a letter is entered it returns a message saying you need to enter numbers in, and am trying to find a way of it checking if there are spaces in the numbers entered at the same time as checking if a number has been entered. My code looks as follows: Code: <html> <head> <title>Activity 2.25</title> </head> <script type="text/javascript"> function validateform() { var element; var BikeMoney; var TVMoney; var iPodMoney; var CarPrice; var flag; flag="OK" element=document.getElementsByTagName('input'); for(counter=0; counter<element.length; counter++) { switch (element[counter].type) { case "submit": break; default: if(isNaN(element[counter].value)) { alert("You will need to enter a number into " + element[counter].name); flag="NotOK" } else { BikeMoney=element[0].value; TVMoney=element[1].value; iPodMoney=element[2].value; CarPrice=element[3].value; } } } if(flag=="OK") if ((Number(BikeMoney)) && (Number(TVMoney)) && (Number(iPodMoney)) && (Number(CarPrice))) { TotalMoney = parseFloat (BikeMoney) + parseFloat (TVMoney) + parseFloat (iPodMoney) if(TotalMoney >= CarPrice) { alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you can afford the car"); } else { alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you cannot afford the car"); } } else { alert("Enter numbers Please"); } } </script> <body> <form name="input form" method="post" action=""> <table> <tr><td>Enter money from bike sale</td><td><input type="text" name="Bike Money"></td></tr> <tr><td>Enter money from TV sale</td><td><input type="text" name="TV Money"></td></tr> <tr><td>Enter money from iPod sale</td><td><input type="text" name="iPod Money"></td></tr> <tr><td>Enter the price of the car</td><td><input type="text" name="Car Price"></td></tr> <tr><td></td><td><input type="submit" value="Submit Details" onclick=validateform()></td></tr> </table> </form> </body> </html> Hai Friends..I made a registration form.I have used php,html as well as javascript in it.In the onclick event of submit button I've provided a call to a function written in javascript.Things are workin fine i.e validations are working properly and insertion of values to the database is also ok. And the problem dat I have is, I want to check whether username and emailid provided is unique with respect to my site...that is no two users can't have same emailid or username.I'm able to retreive the entire emailid and username out of the database.But I don't know where should I place the code for checking the equality.I think that the code should be placed in the javascript validation function..but the code for retreiving values from db is in php. I'm totally confused.I'm attaching here the registration page that I made...anyone out there please go through it and help me with a solution. Hai Forum, May be this question is not in correct format, I want to know about how to check a particular file is exists or not ? using Javascript.One thing without using ActiveXObject because it is supports in IE only. Thanking You, Santosh Srinivas.G Hi, im having a little difficulty checking if an XML node has a value, here the code: var Divs=new Array("artist","bio","img","date","tickets","venue","street","city","country","headliner"); xmlDoc=xmlhttp.responseXML; for ( nodes in Divs ) { if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue) { } else { document.getElementById(Divs[nodes]).innerHTML= xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue+"<br>"; } } This always throws up the error: document.getElementById(Divs[nodes]) is null ive tried putting the xmlDoc in a variable then checking if its null but no luck, also tried the same method against "undefined" but no luck either. Would be greatful if anyone has any suggestions. Thanks, Tom. Hi! I'm new to this forum and almost completely new to programming of any kind, so this may be a very easy, obvious fix; I'm just not sure. Below is a very simple script I wrote partially based on random example scripts. I think an error is generated when the while loop is first executed, because any code I place after the loop is not executed. My guess is that when null is returned, it crashes and stops executing the code. Is that the problem? If so, do you have any ideas for how I could fix it? Thanks in advance for your help! HTML: Code: <a href="javascript:expand(document.getElementById('exp1'))">SeeText</a> <div id="exp1" style="display:none"> <p>SomeText</p> </div> <a href="javascript:expand(document.getElementById('exp2'))">SeeText</a> <div id="exp2" style="display:none"> <p>SomeText</p> </div> Javascript: Code: function expand(param) { i=0; id="exp1"; if (param.style.display=="none") { while (getElementByID(id)) //This seems to be the trouble area. { document.getElementById(id).style.display="none"; i++; id="exp"+String(i) } } param.style.display=(param.style.display=="none")?"":"none"; } I have a javascript that should convert any date into a number between 1 and 260. Based on the outcome of that calculation, a viewer is directed to a specific web page. How can I verify the calculation? How can I see what number javascript is arriving at so I can backtrack to the error? It is not serving up the correct page. I have compared the results to a calculator on another website that uses a different script to calculate the same result -- that's why I am saying the end results are not correct. On the other website, scroll down to where you enter a date. It returns a name just above the date (example: July 25, 1970 returns Electric Star). The script I included above returns Yellow Sun. July 25, 1970 should = 68. Code: function calculate() { if(!isValidDate()) return; var result = 0; var cuttingAge = 260; var yearBox = document.getElementById("year"); var year = yearBox.value; var month = document.getElementById("month").value; var day = document.getElementById("day").value; //Rule # 1 if(month==1 || month==2){ var selectedYearIndex; for(var i=0; i<yearBox.options.length; i++){ if(yearBox.options[i].selected){ selectedYearIndex = i; break; } } year = yearBox.options[selectedYearIndex-1].value; } //Rule # 2 var monthLength = new Array(); monthLength[0]=0; monthLength[1]=31; monthLength[2]=29; monthLength[3]=31; monthLength[4]=30; monthLength[5]=31; monthLength[6]=30; monthLength[7]=31; monthLength[8]=31; monthLength[9]=30; monthLength[10]=31; monthLength[11]=30; monthLength[12]=31; monthLength[13]=31; monthLength[14]=29; var dayPassed = 0; var tempMonth = month; if(month<3) //If its january or february tempMonth = parseInt(tempMonth) +12; //Add previous year's days for(var i=3; i<tempMonth; i++){ dayPassed += monthLength[i]; } dayPassed += parseInt(day) -1; dayPassed = dayPassed%cuttingAge; //Rule # 3 result = parseInt(year) + parseInt(dayPassed); //Rule # 4 if(result > cuttingAge) result -= cuttingAge; //Rule # 5 var pendant = new Array(); var abc = new Array(); pendant[0] = { name:"imix", values:new Array(1, 21, 41, 61, 81, 101, 121, 141, 161, 181, 201, 221, 241) }; pendant[1] = { name:"ik", values:new Array(2, 22, 42, 62, 82, 102, 122, 142, 162, 182, 202, 222, 242) }; pendant[2] = { name:"akbal", values:new Array(3, 23, 43, 63, 83, 103, 123, 143, 163, 183, 203, 223, 243) }; pendant[3] = { name:"kan", values:new Array(4, 24, 44, 64, 84, 104, 124, 144, 164, 184, 204, 224, 244) }; pendant[4] = { name:"chicchan", values:new Array(5, 25, 45, 65, 85, 105, 125, 145, 165, 185, 205, 225, 245) }; pendant[5] = { name:"cimi", values:new Array(6, 26, 46, 66, 86, 106, 126, 146, 166, 186, 206, 226, 246) }; pendant[6] = { name:"manik", values:new Array(7, 27, 47, 67, 87, 107, 127, 147, 167, 187, 207, 227, 247) }; pendant[7] = { name:"lamat", values:new Array(8, 28, 48, 68, 88, 108, 128, 148, 168, 188, 208, 228, 248) }; pendant[8] = { name:"muluk", values:new Array(9, 29, 49, 69, 89, 109, 129, 149, 169, 189, 209, 229, 249) }; pendant[9] = { name:"oc", values:new Array(10, 30, 50, 70, 90, 110, 130, 150, 170, 190, 210, 230, 250) }; pendant[10] = { name:"chuen", values:new Array(11, 31, 51, 71, 91, 111, 131, 151, 171, 191, 211, 231, 251) }; pendant[11] = { name:"eb", values:new Array(12, 32, 52, 72, 92, 112, 132, 152, 172, 192, 212, 232, 252) }; pendant[12] = { name:"ben", values:new Array(13, 33, 53, 73, 93, 113, 133, 153, 173, 193, 213, 233, 253) }; pendant[13] = { name:"ix", values:new Array(14, 34, 54, 74, 94, 114, 134, 154, 174, 194, 214, 234, 254) }; pendant[14] = { name:"men", values:new Array(15, 35, 55, 75, 95, 115, 135, 155, 175, 195, 215, 235, 255) }; pendant[15] = { name:"cib", values:new Array(16, 36, 56, 76, 96, 116, 136, 156, 176, 196, 216, 236, 256) }; pendant[16] = { name:"caban", values:new Array(17, 37, 57, 77, 97, 117, 137, 157, 177, 197, 217, 237, 257) }; pendant[17] = { name:"etznab", values:new Array(18, 38, 58, 78, 98, 118, 138, 158, 178, 198, 218, 238, 258) }; pendant[18] = { name:"cauac", values:new Array(19, 39, 59, 79, 99, 119, 139, 159, 179, 199, 219, 239, 259) }; pendant[19] = { name:"ahau", values:new Array(20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260) }; var redirecctPages = new Array(); redirecctPages["chuen"] = "http://store.mayankin.com/blue-monkey/"; redirecctPages["eb"] = "http://store.mayankin.com/yellow-human/"; redirecctPages["ben"] = "http://store.mayankin.com/red-skywalker/"; redirecctPages["ix"] = "http://store.mayankin.com/white-wizard/"; redirecctPages["men"] = "http://store.mayankin.com/blue-eagle/"; redirecctPages["cib"] = "http://store.mayankin.com/yellow-warrior/"; redirecctPages["caban"] = "http://store.mayankin.com/red-earth/"; redirecctPages["etznab"] = "http://store.mayankin.com/white-mirror/"; redirecctPages["cauac"] = "http://store.mayankin.com/blue-storm/"; redirecctPages["ahau"] = "http://store.mayankin.com/yellow-sun/"; redirecctPages["imix"] = "http://store.mayankin.com/red-dragon/"; redirecctPages["ik"] = "http://store.mayankin.com/white-wind/"; redirecctPages["akbal"] = "http://store.mayankin.com/blue-night/"; redirecctPages["kan"] = "http://store.mayankin.com/yellow-seed/"; redirecctPages["chicchan"] = "http://store.mayankin.com/red-serpent/"; redirecctPages["cimi"] = "http://store.mayankin.com/white-world-bridger/"; redirecctPages["manik"] = "http://store.mayankin.com/blue-hand/"; redirecctPages["lamat"] = "http://store.mayankin.com/yellow-star/"; redirecctPages["muluk"] = "http://store.mayankin.com/red-moon/"; redirecctPages["oc"] = "http://store.mayankin.com/white-dog/"; var pendantName; for(var i =0; i<pendant.length; i++){ var found = false; for(var j=0; j<pendant[i].values.length; j++){ if(result == pendant[i].values[j]){ pendantName = pendant[i].name; found = true; break; } } if(found) break; } document.location = redirecctPages[pendantName]; } /** * Comment */ function isValidDate() { var year = document.getElementById("year").value; var month = document.getElementById("month").value; var day = document.getElementById("day").value; if(month==2 && day>29){ alert("Please select a valid date"); return false } if((month==4 || month==6 || month==9 || month==11) && day>30){ alert("Please select a valid date"); return false } return true; } I have a small webpage which checks for the dimensions of a screen before loading a website, then loading one (of several) which would fit the screen best. Originally this code was fine, until I decided to mesh with it some code that would open a window to specific dimensions and then some other code to remove scroll bars and menus... now leaving me just the lower option which works properly. I would like to sort this out so that each page loads as it should, in accordance with the conditions. Presently only the bottom part of the code works: "if (res!='1' && res!='2' && res!='3') PopupCenter ('http://www.meta.projectmio.com/800.html','MyWindow',800,531)" So the window will open at 800x531 and load ../800.html, but the other conditions will not work, unless I c&p them to this lower part of the code. I think I've gotten something wrong. website: www.meta.projectmio.com/pop.html Thanks. |