JavaScript - Why Doesn't Switch Statement Work With Object Properties?
Hey everyone, I'm a newbie writing a tic tac toe program using OOP. It was simple setting it up so that a player could click on a box to put an X or an O there, but I've run into serious issues when trying to make it so a player couldn't overwrite the AI's choice and the AI couldn't overwrite the player's.
An example of this would be if I made the top right box an X and the AI then made the center box an O, and then I accidentally clicked the center box and made it into an X. I want to prevent that. Every box on the grid is an object with the property of "taken" that helps the program know if a box is empty or not, so as to avert any overwriting in the first place. If the box is empty, this.taken = 0. If the box is filled by a player, taken = 1. If filled by AI, taken = 2. I made it matter whether it was AI or human so later i can check if one of them got tic tac toe. Anyway, by default the constructor class sets this.taken = 0. But the method for checking availability and writing the X or O uses a switch which checks if taken = 0. If it is, it sets taken to either 1 or 2 and writes the necessary symbol. If taken = 1 or 2, it just alerts the player that the spot is taken. But for some reason the switch statement can't tell when taken = anything other than 0. It always executes the code for 0 even though the code for 0 inherently makes it so that taken never equals 0 again, which means case 0 cannot happen anymore. Below is the code with notes. Code: function main(input){//start of main function// function Click(who, where, what){ //this is the method for checking if it's taken or not and writing the X or O if it isn't// /*the argument who represents a number. 0 is no one, 1 is human, 2 is AI; where is a string literal representing the spot on the grid*/ switch(this.taken){ case 0: this.taken = who; document.getElementById(where).innerHTML = what; break; case 1: alert("this spot is taken"); break; case 2: alert("this spot is taken"); }//end switch }//end Click function Box(inputhere){//start of class// this.taken = 0; this.pick = Click; } //end of Box class// //object declarations (I cut out most of them and left only the relevant ones// var topleft = new Box(); var topmid = new Box(); var topright = new Box(); var centerright = new Box(); //end of object declarations// switch (input){ /*in each .pick(), the first arg is whether or not a player chose it (1 = player did, 2 = comp did). The second arg is which box and the third is whether to put an X or O. The input variable in the switch statement is an argument passed through main() when the player clicks on a box. Topleft passes 1.1, topmid passes 1.2 and so on.*/ case 1.1:{ //The first instance of .pick() in each case is what the player did. The second is what the AI will do in repsonse.// topleft.pick(1, "topleft", "X"); topmid.pick(2, "topmid", "<span>O</span>"); break; }//end of case 1.1 case 1.3:{ topright.pick(1, "topright", "X"); centerright.pick(2, "centerright", "<span>O</span>"); break; }//end of case 1.3 }//end of switch }//end of main// Is there anyone who has any idea why on earth this is happening? I've been at it for an embarrassing amount of hours. Also, thanks to anyone who even considers helping : ) (feel free to flame me if my code sucks or my post is too long or anything). Similar TutorialsHi! I have encountered a problem with IE that I'm simply unable to solve. I have a form where the user can choose different things to input from a drop down list and depending on the choise, different kinds of textboxes of text areas etc. are loaded onto the page with JS. After the user has finished inputing text and submits the form, I read the input with PHP and process it further. Now, this works perfectly in firefox but IE doesn't seem to add the name properties to the elements (both textboxes and text areas) because PHP cannot find them and no info is printed from the input. The creation of the elements (adding them to the page...) works just fine, it's just getting the data from them that's the problem. Since it works in FF I know it's not a PHP problem. I've used the recommended .name to set the property (although I've also tried .setAttribute() etc), yet it still doesn't work. What can I do to solve this? My relevant JS code: Code: function addTextbox(idName, head) { var target = document.getElementById('addThings'); var newDiv = document.createElement("div"); newDiv.id = "container"; newDiv.name = "container"; newDiv.setAttribute("className", "intNew"); //IE newDiv.setAttribute("class", "intNew"); //FF var newTextbox = document.createElement("input"); newTextbox.type = "text"; newTextbox.id = idName; //-- newTextbox.name = idName; //Doesn't work in IE?.. newTextbox.setAttribute("className", "newWidth"); //IE newTextbox.setAttribute("class", "newWidth"); //FF var text = document.createTextNode(head + ":"); target.appendChild(newDiv); newDiv.appendChild(text); newDiv.innerHTML += "<br />"; newDiv.appendChild(newTextbox); newDiv.innerHTML += "<p />"; } (The text area function is the same, more or less) The PHP code, if anyone's interested: Code: if($_POST['createBtn']) { $head = $_POST['head']; //1 $intro = $_POST['intro']; //2 $question = $_POST['question']; //3 $answer = $_POST['answer']; //3 $image = $_POST['image']; //? $author = $_POST['author']; //5 $end = $_POST['end']; //4 //sammanfattning printHTMLTop(9); $today = date('Y-m-d'); $text = <<<END <div class="intContainer"> <div class="intHeadRow"><b>$head </b></div> <div class="stpdIEContainer"> <div class="intTextContainer"> <p /> $intro <p /> END; if($question != "" && answer != "") { foreach($question as $k) { $text .= $k . "<p />"; foreach($answer as $j) { $text .= $j . "<p />"; } } } $text .= <<<END <p /> $end <p /> <i>Skrivet av: $author den $today</i> </div> <!-- intTextContainer --> <div class="intImgContainer"> </div></div> <!-- stpdIEContainer --> </div> <!-- intContainer --> END; //Prints to new file (on server) $file = file_put_contents('interviews/interview01.html', $text); //Set name //Add to DB //print newly created file $page = file_get_contents('interviews/interview01.html'); echo $page; printHTMLBottom(); My switch function just goes to the default. I've double double checked the file paths of the image on the page and it's right. So I'm not sure what's going on. here's what I got: Code: function change2(picName,imgName) { switch (document[picName].src) { case document[picName].src= "images/leaf_shapes/leaf_auriculate.gif": document[picName].src = "images/leaf_shapes/crenate/leaf_elliptic_crenate.gif"; break; case document[picName].src= "images/leaf_shapes/leaf_cordate.gif": document[picName].src= "images/leaf_shapes/serrate/leaf_elliptic_serrate.gif"; break; default: document[picName].src= "images/leaf_shapes/transparent1.gif"; } } Ive searched on this board but cant find out why this doesn't work. Code: <script type="text/javascript"> function showSelected(val) if (val == "Nil") { document.getElementById('JustText').innerHTML='hi' } else { document.getElementById('selectedResult').innerHTML="<a href='mailto:" + val + "'>" + val + "</a><p>" } </script> I have a page with a Geo IP redirect that's supposed to redirect users from London to URL#1 and the rest to URL#2. It's an external geo ip lookup service. First comes the IP lookup: Code: <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"> /* GeoIP Deny Access by City and Redirect Javascript 1.0 http://wiki.category5.tv/MaxMind_GeoIP_API API (c) MaxMind - www.maxmind.com - used with permission "GeoIP Deny Access by City" script by Robbie Ferguson, www.Category5.TV You are free to use and share this script, however this notice must remain intact. */ </script> And then, and here's the problem I think, is the redirects inside an if/else: Code: <script type="text/javascript"> var city=new Array("London, H9") var redirect="http://www.URL1.com" var redirect2="http://www.URL2.com" /* do not edit past this line */ Array.prototype.inArray = function(q) { for(i in this) { if(this[i].toUpperCase() === q) return true; } } var myCity=geoip_city().toUpperCase() var myRegion=geoip_region().toUpperCase() if(city.inArray(myCity+", "+myRegion)) { window.location = redirect; } else { window.location = redirect2; } The redirect works if you are indeed from London. So if the if-statement is true, "window.location = redirect" works, but if the statement is not true, "window.location = redirect2" doesn't seem to be called. Help would be extremely appreciated I created a method for displaying an object's properties: Code: renderfunction = false; function showProperty (object, property) { document.write ('<td class="type">' + (typeof object[property]) + '</td>' + '<td class="name">' + property + '</td>'); document.writeln('<td class="value">' + ( (typeof object[property] != 'function') ? object[property] :( (property != 'showProperties') ? ( renderfunction ? object[property]() : ('<span class="self">NOT RENDERED</span>') ) : ('<span class="self">THIS</span>') ) ) + '</td>'); document.writeln('<td class="hasOwnProperty" >' + ( object.hasOwnProperty(property) ? "Local" : "Inherited" ) + '</td>'); if (typeof object[property] == 'function') { document.writeln ('<td class="function">' + object[property] + '</td>'); } else { document.writeln ('<td class="function"> </td>'); } } As long as renderfunction = false, the object is fine coming out of this function. However, if I change renderfunction to true, all my properties become undefined. Why isn't this working as I expect it to? How should I fix it? Thanks in advance, -Brian. I'm not sure I phrased the question properly and it's probably easiest to show what I'm trying to get. Basically, I want to find all the properties(?) available for a single track object in iTunes. For example, this is how I access what's stored as the Album and Artist for a track: Code: var myTracks = myPlaylists(1).Tracks; alert(myTracks(1).Album); alert(myTracks(1).Artist); There are a ton of properties (like Album and Artist), but I don't know what they all are, so I was trying to list them like this, but it doesn't work: Code: for(var key in myTracks(1)) alert(key); It's probably obvious why this doesn't work, but does anyone know how to get this list? Thanks for any help! Not sure if I even ask correctly. Excuse the poor PHP man for not knowing javascript. I need to add a check to this code. It picks up an image when you click on it, then puts it down when you click again. What it needs is another check WHERE (on what) have you clicked to put it down. If it's a particular DIV then OK. If not (if elsewhere) don't put it down and don't change the click counter. Code: <html> <head> <style type="text/css"> .drag { position: relative; height:56px; width:56px; background-color:#FF0000; color:White; } </style> <script type="text/javascript"> var _startX = 0; // mouse starting positions var _startY = 0; var _offsetX = 0; // current element offset var _offsetY = 0; var _dragElement; var _oldZIndex = 0; // we temporarily increase the z-index during drag var _debug; var _countClick = 0; InitDragDrop(); function InitDragDrop() { document.onclick = OnMouseClick; document.onMouseClick = OnMouseClick; } function OnMouseClick(e) { debugger; _debug = $('debug'); // if first click this is zero if (_countClick == 0) { // IE if (e == null) e = window.event; // IE uses srcElement, others use target var target = e.target != null ? e.target : e.srcElement; _debug.innerHTML = target.className == 'drag' ? 'draggable element clicked' : 'NON-draggable element clicked'; // for IE, left click == 1 // for Firefox, left click == 0 if ((e.button == 1 && window.event != null || e.button == 0) && target.className == 'drag') { // grab the mouse position _startX = e.clientX; _startY = e.clientY; // grab the clicked element's position _offsetX = ExtractNumber(target.style.left); _offsetY = ExtractNumber(target.style.top); // bring the clicked element to the front while it is being dragged _oldZIndex = target.style.zIndex; target.style.zIndex = 10000; // set _dragElement for next click _dragElement = target; // tell our code to start moving the element with the mouse document.onmousemove = OnMouseMove; // cancel out any text selections document.body.focus(); // prevent text selection in IE document.onselectstart = function() { return false; }; // prevent IE from trying to drag an image target.ondragstart = function() { return false; }; //set click count to 1 so we know next click is to release _countClick = 1; // prevent text selection (except IE) return false; } } if (_countClick == 1) { if (_dragElement != null) { _dragElement.style.zIndex = _oldZIndex; // we're done with these events until the next OnMouseDown document.onmousemove = null; document.onselectstart = null; _dragElement.ondragstart = null; // this is how we know we're not dragging _dragElement = null; _debug.innerHTML = 'mouse up'; } //set click count back to 0 so next we know it's first click again _countClick = 0; } } function OnMouseMove(e) { if (e == null) var e = window.event; // this is the actual "drag code" _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px'; _dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px'; _debug.innerHTML = '(' + _dragElement.style.left + ', ' + _dragElement.style.top + ')'; } function ExtractNumber(value) { var n = parseInt(value); return n == null || isNaN(n) ? 0 : n; } // this is simply a shortcut for the eyes and fingers function $(id) { return document.getElementById(id); } function OnMouseUp(e) { } </script> </head> <body> <div id="thediv" class="drag" style='background: url(shield.gif) no-repeat center top;'></div> <pre id="debug"> </pre> <div id="slot1" class="dropon" style='position: absolute; height:28px; width:28px; top: 200px; left: 200px; background-color:#FF0000; color:White;'></div> <div id="slot2" class="dropon" style='position: absolute; height:28px; width:28px; top: 200px; left: 230px; background-color:#FF0000; color:White;'></div> </body> </html> Do many programmers remember most of the object properties and methods or do they use IDE or references to find those specific objects. I'm starting to learn Javascript and seeing all the different type of objects available can be depressing. I have something like this var oneTest = new CustomObj({ prop1 : 'value1', prop2 : 'value2', prop3 : 'value3' }) var twoText = Object.clone(oneTest) twoText.prop2 = "newvalue2" And when I console log twoText I see something like +Data prop2 Inside Data is a prop2 that has the value of "value2". THAT is the one I want to change/override... yet the console shows me that the prop2 is outside of the data structure so when I am acting on the cloned obj I am not getting the results i need. I tried obj.extend etc.... and that didn't work, perhaps my syntax was wrong. Any advice? Hello everyone, I am fairly familiar with the concept of Objects and their properties and methods, but javascript being object based as opposed to object oriented has me stumped on how to access an object's properties from an onclick event handler created for another object created within the original object. In the example below, I have a constructor function called anyObj. to which I pass an object reference to an element. anyObj has 3 properties and one function increaseWidth() increaseWidth() creates a new button with an onclick event handler and this is where I have a problem. The onclick function needs to increase the value of anyObj's this.width property. I originally had a line this.width += 10; in the onclick but quickly realised why this wasn't working because the this in the onclick function refers to the new button object and not the this.width property of anyObj. The workaround I have used, and it works, is to make a copy of all the this.xxxxx properties. eg. width = this.width; and use the width variable in the onclick as you can see below. This "workaround" works fine but doesn't feel ideal to me. So, what I am asking advice on is, is there a better way to access the anyObj()'s properties from within the onclick function than the way I have done it? Obviously I would prefer to not have to make copies of all the anyObj() properties like I have to make them accessible to the onclick function. Code: function anyObj(divObj){ this.elem = divObj; this.width = 50; this.height = 50; this.increaseWidth=function(){ width = this.width; height = this.height; //create a button for this object to be appended to an element later on var newButton = document.createElement('button'); newButton.onclick=function(){ width += 10; //... //... } //... //... } } Hi everyone, I'm a beginner who has now come across the switch statement and has been trying to understand it with this simple coding i came up with. I think I have the syntex of the switch command correct but I'm trying to get it to work along with a HTML form and a function. I've been trying to figure out what i am do wrng but can not seem to see the solution. can someone guide me to the solution? thanks John Code: <html> <head> <title>Using the switch statement.</title> <script type="text/javascript"> function basegraincheck(){ var basegrain = document.basegrainform.basegrain1.value return basegrain } switch (basegrain) { case 1 : displaygrain = "US Pale ale malt" break case 2 : displaygrain = "Maris Otter Malt" break case 3 : displaygrain = "Crystal Malt" break default: displaygrain = "somethings wrong with the switch statement" } document.write(displaygrain); </script> </head> <body> <form name="basegrainform" action="" method="get" onsubmit="basegraincheck()"> <h2>Select a grain</h2> <br> <p>Pale ale Malt</p> <input type="radio" name="basegrain1" value="1"> <br> <p>Maris Otter Malt</p> <input type="radio" name="basegrain1" value="2"> <br> <p>Crystal Malt</p> <input type="radio" name="basegrain1" value="3"> <br> <input type="submit" value="Submit"> </form> </body> </html> Here i am attempting to connect to my javascript two input fields one number and one radio button to change the function computeBudget()... Thank you in advance im desperate for over 48 hours 3 days with trial and error and looking for tutorials for this kind of situation. Code: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <script> function computeBudget() { var income = document.getElementById('income').value; switch (income) { case "Weekly": this.add((income * 52) / 12); break; case "biWeekly": this.add((income * 26) / 12); break; } var grocery_expenses = document.getElementById('grocery_expenses').value; var car_insurance = document.getElementById('car_insurance').value; var home_mortgage = document.getElementById('home_mortgage').value; var home_utilities = document.getElementById('home_utilities').value; var life_insurance = document.getElementById('life_insurance').value; var monthgain = (income - grocery_expenses - car_insurance - home_mortgage - home_utilities - life_insurance).toFixed(2); monthgain = monthgain.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); document.getElementById('monthgain').innerHTML = "Monthly Net Gain = $" + monthgain; var yeargain = ((income - grocery_expenses - car_insurance - home_mortgage - home_utilities - life_insurance) * 12).toFixed(2); yeargain = yeargain.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); document.getElementById('yeargain').innerHTML = "Yearly Net Gain = $" + yeargain; } </script> </head> <body> <p>Monthly Income: $<input name="income" id="income" type="number" min="1" max="100000000" onchange="computeBudget()"></p> <p>Weekly <input name="income" id="income" type="radio" value="Weekly" onchange="computeBudget()">BiWeekly <input name="income" id="income" type="radio" value="biWeekly" onchange="computeBudget()"></p> <p>Grocery Expenses: $<input id="grocery_expenses" type="number" min="1" max="100000000" onchange="computeBudget()"></p> <p>Car Insurance: $<input id="car_insurance" type="number" min="1" max="100000000" onchange="computeBudget()"></p> <p>Home Mortgage: $<input id="home_mortgage" type="number" min="1" max="100000000" onchange="computeBudget()"></p> <p>Home Utilities: $<input id="home_utilities" type="number" min="1" max="100000000" onchange="computeBudget()"></p> <p>Life Insurance: $<input id="life_insurance" type="number" min="1" max="100000000" onchange="computeBudget()"></p> <h2 id="monthgain"></h2> <h2 id="yeargain"></h2> </body> </html> Reply With Quote 12-27-2014, 11:30 PM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,311 Thanks 82 Thanked 4,754 Times in 4,716 Posts Makes no sense. You have this code in the computeBudget() function: Code: this.add((income * 52) / 12); (1) The value of this depends on what invoked the computeBudget function. You invoke it from radio buttons and from text fields. (2) You use a method named add( ) on each of these various fields. There is NO SUCH METHOD defined for either radio buttons or text fields. I have no idea what you intend to do with that value you compute (e.g., ((income * 52) / 12)) but you sure as heck can't add it to anything (unless you write your own method or function named add) and you certainly can't use it to any reasonable effect on this, especially when this refers to a radio button. Im creating a switch statement for this case, but dont know how to start - Create a Switch statement that tests the value of the partyType parameter. If partyType equals "D", store the following text string in a variable named barText: Code: <td class='dem'> </td> If partyType equals "R", barText should equal Code: <td class='rep'> </td> etc.. So since its going to test the value of the partyType parameter, and store the text string in a variable named barText do i begin like: Code: var barText switch (partyType) and how do I write the equals "D", etc. the only examples I have are Code: if (seating = = 'B') doc.write ("Your seat is in the balcony") ^ would i write it like that except seating would = partyType and "Your seat in the balcony" would equal the "<td class etc." ie Code: if (partyType = = "D") doc.write ("<td class='dem'> </td>") Hi; Please can somebody help me make this code work for me. i want it to work in such a way that when someone select the ages in the combo box,it will direct the person to another page. Here is it <html> <body> <script type="text/javascript"> page = document.frmOne.cmbAge.value switch (page) { case "1": document.URL = "page1.html" break case "2": document.URL = "page2.html" break case "3": document.URL = "page3.html" break case "4": document.URL = "page4.html" break default: alert("An error occurred, so we are staying here") } </script> </body> <form name="frmOne"> <select name = "cmbAge"> <option value = 1>5 to 16</option> <option value = 2>17 to 30</option> <option value = 3>31 to 45</option> <option value = 4>46+</option> </select> </form> </body> </html> Please help me solve this.. Thanks you. Clement Osei. Hi, im new to this so i was wondering if anyone could tell me where im going wrong? im trying to get my switch statement to work and give me a specific reply for 4 names and if one of these names isnt typed into the promt box then i want an alert box to appear but i cant seem to get it to work. any ideas? <body> <script type="text/javascript"> //<![CDATA[ var Card_Suit; Card_Suit=window.prompt("Think of a card suit? What suit are you thinking of?","????"); //]]> </script> <h1 style='color: blue'>Your Future In The Cards</h1> <script type="text/javascript"> //<![CDATA[ switch(Card_Suit) { case "clubs": {document.write("<p style='color: black'>"); document.write("Beware of three legged dogs crossing your path</p>"); } break; case "spades": {document.write("<p style='color: black'>"); document.write("Wear brown with pink spots for luck</p>"); } break case "hearts": {document.write("<p style='color: red'>"); document.write("You are going to meet a stranger with two heads</p>"); } break; case "diamonds": {document.write("<p style='color: red'>"); document.write("Your lucky number for today is 13,254,297</p>"); } break; } else window.alert("this is not a card"); } Hello all, This is my first post here, so I apologize if I didn't format this correctly. I am trying to figure out where I went wrong in my code. The objective is to display all lines of the song using a switch statement. I thought I had my head wrapped around it, but apparently not. Would anyone mind pointing me in the right direction, or telling me what I did wrong here? I would be greatly appreciative! Thank you for your consideration folks. Code: <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Excercise 8.11</title> <script type = "text/javascript"> for (var day = 1; day <=5; day++) { switch (day) { case 1: document.write("On the "+day+"st of Christmas, my true love gave to me, A partridge in a pear tree."); case 2: doucment.write("On the "+day+"th day of Christmas, my true love gave to me, Two turtle doves,<br>"); case 3: document.write("On the "+day+"th day of Christmas, my true love gave to me, Three French hens,<br>"); case 4: document.write("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds,<br>"); case 5: document.write("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings,<br>"); break; } } </script> </head> <body> </body> </html> Hi, I'm trying to code a script that will display an approximate postage price based on different combinations of variables, but the numbers I defined for each case aren't reflected in the text box. I'm using a switch statement for all the different combinations of options, which the user will choose by selecting check-boxes. I'd really appreciate if anyone could point out where I'm going wrong. (Additionally, I know that this code probably isn't a very efficient way of doing what I'm doing - I'm open to suggestions on how to improve it.) I've attached all the relevant parts of the code, including how I defined variables originally. Many thanks in advance, Gil Code: <script type="text/javascript"> function count() { var firstclass = document.calc.firstclass.value; var postcard = document.calc.firstclass.value; var numpages = document.calc.numpages.value; var nms = document.calc.nms.value; var large = document.calc.large.value; var numpages = parseInt(document.calc.numpages.value); if (document.calc.grabber.checked) { var vweight = (numpages + 8) * 0.17636981; } else { var vweight = numpages * 0.17636981; } var postageprice = 0; switch(postageprice) { case document.calc.postcard.checked: var postageprice = 28; break; case document.calc.firstclass.checked && vweight <= 1: var postageprice = 44; break; case document.calc.firstclass.checked && vweight <= 2: var postageprice = 61; break; case document.calc.firstclass.checked && vweight <= 3: var postageprice = 78; break; case document.calc.firstclass.checked && vweight <=3.5: var postageprice = 95; break; case document.calc.firstclass.checked && document.calc.large.checked && vweight <= 1: var postageprice = 88; break; case document.calc.firstclass.checked && document.calc.large.checked && vweight <= 2: var postageprice = 105; break; case document.calc.firstclass.checked && document.calc.large.checked && vweight <= 3: var postageprice = 122; break; case document.calc.firstclass.checked && document.calc.large.checked && vweight <= 4: var postageprice = 139; break; case document.calc.firstclass.checked && document.calc.nms.checked && vweight <= 1: var postageprice = 64; break; case document.calc.firstclass.checked && document.calc.nms.checked && vweight <= 2: var postageprice = 81; break; case document.calc.firstclass.checked && document.calc.nms.checked && vweight <= 3: var postageprice = 98; break; case document.calc.firstclass.checked && document.calc.nms.checked && vweight <= 3.5: var postageprice = 105; break; case document.calc.firstclass.checked && document.calc.nms.checked && document.calc.large.checked && vweight <= 1: var postageprice = 122; break; case document.calc.firstclass.checked && document.calc.nms.checked && document.calc.large.checked && vweight <= 2: var postageprice = 139; break; case document.calc.firstclass.checked && document.calc.nms.checked && document.calc.large.checked && vweight <= 3: var postageprice = 156; break; case document.calc.firstclass.checked && document.calc.nms.checked && document.calc.large.checked && vweight <= 4: var postageprice = 173; break; default: var postageprice = 0 } document.calc.pay.value = postageprice } </script> <form name="calc" method="POST"> <input type="text" name="numpages" size="10" value=10 onpropertychange="count()" onkeypress="keypress(event)" onclick="SelectAll()"> <input type=button name=clearnumber value="Clear" onclick="document.calc.numpages.value=0"> <p></p> <input type="checkbox" name="firstclass" size="10" onclick="count()"><p> <input type="checkbox" name="large" size="10" onclick="count()"></p> <p> <input type="checkbox" name="nms" size="10" onclick="count()"></p> <p> <input type="checkbox" name="postcard" size="10" onclick="count()"></p> <p> <input type="checkbox" name="grabber" size="10" onclick="count()"></p> <p>$<input type="text" name="pay" size="10"> $<input type="text" name="perthousand" size="10"> </p> </form> Thanks again. Hi my goal was to archive a switch statement in javascript to check the users IE versions. Allowing me to switch my javascript code to which will only work in other versions. Yet I don't know if a switch would be appropriate in this matter. Any tips ore suggestions are more then welcome. Please let me know if a if statement would be better. it involves large javascript code. Code: <script type="text/javascript" language="javascript"> if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { var ieversion = new Number(RegExp.$1); switch (ieversion) { case 8: document.write("IE 8") break; case 5: document.write("IE 5") break; case 6: document.write("IE 6") break; case 7: document.write("IE 7") break; } } </script> I'm still learning js core using Flanagan's 5th ed. so please bear with me; is the if ("ignore" in arguments.callee) return; below valid or a typeo? Thanks J. Code: function inspect(inspector, title) { var expression, result; // You can use a breakpoint to turn off subsequent breakpoints by // creating a property named "ignore" on this function. if ("ignore" in arguments.callee) return; ....... " <body> <a href="biography.html" id="biography">BIO</a> <script type="text/javascript"> window.onload = initRedirect; function initRedirect() { switch(getElementById.onclick) { case "getElementById"biography".onclick": alert("Four score and seven years ago..."); return false; default: } } </script> </body> " I don't quite understand the getElementById property. Is there a way I can get it so that I click on the biography link and an alert displays? |