JavaScript - Callbacks, If Statements? Best Approach
Hey guys, here's my code
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script src="jquery-1.3.2.min.js" type="text/javascript"></script> <style type="text/css"> #box { background: red; width: 300px; height: 300px; display: none; } #box2 { background: green; width: 300px; height: 300px; display: none; } </style> <script type="text/javascript"> $(function() { $('a.about').click(function() { $('#box').stop().slideToggle(2000); }); }); $(function() { $('a.contact').click(function() { $('#box2').stop().slideToggle(2000); }); }); </script> </head> <body> <div id="box"></div> <div id="box2"></div> <a href="#" class="about" />about</a> <a href="#" class="contact" />contact</a> </body> </html> Basically I want to make it so that if you click about, and about is open, once you click contact it closes about before opening contact? I'm not sure how to approach this though whether I should use if statements or something? I'm pretty new to jquery Similar TutorialsHey guys. I have a weird issue (you kind of have to see it for yourself to understand). If you double click on the next or previous button quickly (within 500 miliseconds of each other) it creates a double of the next slide. http://insidehb.com/imageofday/ Let me know if you can help. Thanks. I'm pretty new to Javascript and just started with some OOP concepts, so this might be an obvious one and I apologize if the question is much longer than it should be... Basically I am trying to create a reusable class object with several internal functions, that is completely abstracted, meaning the class object itself should never have to reference the global declarations or window object. This class should be aware of itself at any point in the execution of it's own functions, and that is what I'm having trouble with at the moment. What I'm trying to do is create reusable javascript "Apps" for my HTML web application, where multiple instances can be instantiated and used independently from one another. My main problem is "this" loses context in the Ajax callback and onclick handlers. I'm not sure how to persist the context of "this", or at least keep a reference to prototype instance itself. Let's set up a simple class called "App" to demonstrate what I mean... Code: var myApp = new App("MyFirstApp"); function App(argName) { this.name = argName; this.GetDirectory("C:\Program Files"); } App.prototype.GetDirectory = function(argPath) { // "this" equals the App object instance (good!), meaning this.name should equal "MyFirstApp" Ajax.GetDirectory(argPath, this.GetDirectoryCallback, this.GetDirectoryTimeout, this.GetDirectoryError); } App.prototype.GetDirectoryCallback = function(argFilePaths) { // "this" equals the "window" object (bad!), meaning this.name should equal a null reference // argFilePaths contains a string of file paths (delimited using ;) returned by the server var mFilePaths = split(argFilePaths, ";"); // for each file path, add a div to the document body containing that file path, with an onclick handler for (var i in mFilePaths) { var mFilePath = mFilePaths[i]; var mFilePathDiv = document.createElement("div"); mFilePathDiv.innerHTML = mFilePath; mFilePathDiv.setAttribute("onclick", "javascript:this.FilePathDivClickHandler();return false;"); document.getElementById("body").appendChild(mFilePathDiv); } } App.prototype.FilePathDivClickHandler = function() { // I need a reference to the App object instance here, but I'm not sure where to grab it... // what I want to do is call GetDirectory again, referencing the div's innerHTML which contains another file path, like this: this.GetDirectory(event.target.innerHTML); } The onclick handler using "this" is obviously not going to work because "this" is a reference to the "window" object which does not have a function called FilePathDivClickHandler(). So what I can do is nest the GetDirectoryCallback() function inside the GetDirectory() function so that GetDirectoryCallback() can reference the variables in the outer function (GetDirectory). Code: App.prototype.GetDirectory = function(argPath) { // "this" equals the App object instance (good!), meaning this.name should equal "MyFirstApp" // "this" will still lose context inside the callback, so we can set up a variable called "instance" that the callback can reference var instance = this; Ajax.GetDirectory(argPath, GetDirectoryCallback, GetDirectoryTimeout, GetDirectoryError); function GetDirectoryCallback(argFilePaths) { // "this" equals the window object (bad!), meaning this.name should equal a null reference // "instance" equals the App object instance (good!), meaning instance.name should equal "MyFirstApp" // argFilePaths contains a string of file paths (delimited using ;) returned by the server var mFilePaths = split(argFilePaths, ";"); // for each file path, add a div to the document body containing that file path, with an onclick handler for (var i in mFilePaths) { var mFilePath = mFilePaths[i]; var mFilePathDiv = document.createElement("div"); mFilePathDiv.innerHTML = mFilePath; mFilePathDiv.setAttribute("onclick", "javascript:instance.FilePathDivClickHandler();return false;"); document.getElementById("body").appendChild(mFilePathDiv); } } } Even though we have persisted the App object instance through the use of the "instance" variable in the outer function, the onclick event handler still does not work; when it fires, "instance" is an unknown object. Would placing the FilePathDivClickHandler() function inside the GetDirectory() function, much like what was done with the callback, allow me to use "instance" correctly for the onclick handler? Hi and hope you can help, I'm doing some course work and the current lesson I'm doing is about substituting images depending on onclick functions. I've attached the HTML / JavaScript as a txt file, this is their sample file and it is rather bulky and repetetive. I was wondering whether it would be possible to simplify the code as there is a lot of repetition, there are 8 question functions and 8 answer functions, I'm wondering if this could be simplified into 1 question and 1 answer function. Thanks for your interest, R I'm learning JavaScript, but want to make an accounting program(site) for my brother. It will be backed by php and mysql. I've found that javascript doesn't "really" have multidimensional arrays. The ways I figure to collect name, address, phone, etc could be any of these: 1. Create a customer object (customer.name, customer.address) 2. Use the mutidimensional array that JavaScript does offer. (var customers [ [], [] ]) 3. Create bunches of variables (var customerName, var customerAddress) or any other method I have found (will find). The information will for a customer will be filled out via HTML forms, processed by JavaScript then sent up to the PHP/MySQL database. OR should I just use PHP to deal with these directly? My impression is to use client side technology as much as possible or is this wrong? -Matt I have the need to utilize a pop-up calendar about three times on one of my pages. I created the calendar in a separate html file, complete with jquery functions for pulling the date once a user clicks their desired day. What is the best approach to have that calendar pop up in small window that hovers on the page so that I can take the users clicked day and utilize it on the main page? Should I use the jquery load () function to load the calendar into a div on the main page? Or should I include the calendar file in my header? I'm just asking for some ideas and guidance. Hi All, I am working on a page with a webform and a subform. I have two related tables where my main form will populate the one side of the relationship and the subform will be for the many side. I am thinking about using an HTML table to hold my subform fields with javascript to dynamically add rows for additional records. I am having trouble with the design of the form processing script, however as since the index on the parent record does not exist at the time of processing I can't think of a way to populate the link field for the child records. The only thing I can think of is to have my page's onload event create a "blank" record (generating the index and storing it in a hidden field) and then essentially make the proceesing script be an update rather than a create. This of course would involve creating some sort of delete function if the user leaves the page without submiting the form. I am not sure this is the best approach but it is all I can come up with. I am open to any ideas or guidance from the community on either my subform idea or processing script idea and I thank you in advance. Kind regards, Ken I'm almost positive this question will make you groan "neewwwbie!".... I have a simple javascript that decides which 'div' to make visible, based on another element within the html. What i want now is to create a page where someone can view all the possible divs in that javascript that might appear and edit them (including href's, ital, bold and image tags.) Much like a CMS system, but the information is within a javascript, not a database. I would assume it's possible. What's a good way to approach this? It sounds like Javascript cannot edit .txt files. Is there a way to have the user access and edit an .xml file and just pull that into the div tag, instead of text? I have many radio buttons associated with questions on a page with Yes/No options. Depending on if Yes or No is selected (varies with question), I need to prevent the user from going off of the page and somehow "mark" the questions that are answered "incorrectly/not preferred". I could probably put validation on each individual radio button and display a message when going off the page, but I'm looking to make this more dynamic. One idea I had was to put a function on each radio button's onChange event and build an array using .push() to construct the array with all the radio button IDs that are answered incorrectly. I ran into problems when deleting from the array when the radio button was changed back to a correct response though. What may be best is to somehow indicate which radio buttons need looked at when leaving the page and then run a function or case statement over all the radio buttons to build an object? I'm not sure what approach to take. Any thoughts are appreciated. Thanks. Im trying to figure out why this code won't work. Unsuccessfully. Sorry, didn't know how to use the "CODE" tag. <html> <head> <script type:"text/javascript"> var BGColor = prompt("Would you like the background of the page to be red, green, or blue?","") </script> </head> <body> <script type:"text/javascript"> if (BGColor == "red") { document.write('<body bgcolor= "red">The body of this page is RED. Press F5 to restart!') } else if (BGCOlor == "green") { document.write('<body bgcolor= "green">The body of this page is GREEN. Press F5 to restart!') } else if (BGColor == "blue") { document.write('<body bgcolor= "blue">The body of this page is BLUE. Press F5 to restart!') } </script> </body> </html> Hi I need help to finish this code with using nested if statements A price of a ticket to a passenger will be: First Class 500 Economy Class (with meal) 400 Economy Class (without meal) 200 How I can write a JavaScript code according to the following specifications: a. Read the class that the passenger wants to travel on. b. If the class is the first class, print the price of ticket. c. If the class is the economy class, ask the user if he/she wants a meal on the flight. Then print the price of the ticket according to the response of the passenger. The program should simply accept one possible strings the user enters; it is not required to work for all possible inputs such as First, first, FIRST or Yes , yes, YES. This is the code which I have been trying { var inputNum = prompt("Enter the class you want\n first class?\neconomy? :"); if (isNaN(inputNum)) { if (inputNum.match(/first class/)) { document.write("<h1><center>your Ticket is 500<\center><\h1>"); } else { prompt("<h1>if you want a meal on the flight press OK <\h1>"); document.write("<h1>your ticket is 400<h1>,"); } } The issue I am having is with my sumAll function: Code: <script type="text/javascript"> /* <![CDATA[ */ function addNumbers() { a = parseInt(document.getElementById('txtFirst').value); b = parseInt(document.getElementById('txtSecond').value); c = parseInt(document.getElementById('txtThird').value); d = parseInt(document.getElementById('txtFourth').value); ansE = document.getElementById("Sum"); ansE.value = '$' + (a + b + c + d); x = (a + b + c + d); if ( x >= 100000) { document.getElementById('Bonus').value = '$' + 1200; } else if ( x > 50000 && x<100000) { document.getElementById('Bonus').value = '$' + 800; } else { document.getElementById('Bonus').value = '$' + 0; } if (document.getElementById('Blue').checked == true && document.getElementById('Gala').checked == true) { document.getElementById('Committee').value = '$' + 800; } else if (document.getElementById('Blue').checked == true && document.getElementById('Gala').checked == false) { document.getElementById('Committee').value = '$' + 350; } else if (document.getElementById('Blue').checked == false && document.getElementById('Gala').checked == true) { document.getElementById('Committee').value = '$' + 450; } else { document.getElementById('Committee').value = '$' + 0; } } function sumAll() { y = parseInt(document.getElementById('Bonus').value); z = parseInt(document.getElementById('Committee').value); total = document.getElementById("Total"); total.value = (y + z); } function funcCalc() { addNumbers(); sumAll(); } /* ]]> */ </script> I'm really new to this and I'm trying to figure out how to sum the two values from if statements. ParseInt is returning NaN. Before that, it was adding my results together as strings. Is there an easy way to add these together without creating a very complicated if statement to total these up? Thanks! Hi, I am trying to detect a users browsers and output a different code based on browser. I found this code online that uses the object detection method and it works. When I document.write(BrowserDetect.browser) it outputs Chrome, (which is what I am using). The code can be found he http://www.quirksmode.org/js/detect.html So I can't understand why the following code does not work? It ouputs the "not" in the else statement. I believe that BrowserDetect is an object and the .browser at the end is a property of the object. Can one not make an object a variable? or make an object a string? oh and BTW there is a bunch of javascript that creates the browserdetect object that can be found on the site above, but I thought it would be too unwieldy to post here. Thanks. The following script outputs: Chrome Not Working <script type="text/javascript"> document.write(BrowserDetect.browser); var browser="BrowserDetect.browser"; if(browser=="Chrome") { document.write(" Working"); } else { document.write(" Not working"); } </script> Hello, I have a long string of if..else statements, and the same is used in multiple functions. How do a make a variable that I can call instead of copying 40 lines of code? Thanks! This may be a basic programing problem but I am writing a function to validate a form using a bunch of else-if statements and encounters a problem when I use nested if/else-if statements. It will not continue on to the next else-if, after it returns false. Code: // to check each entry on the form and alert user if entry is invalid. function checkform(){ var checkssn = /(\d{3})-(\d{2})-(\d{4})/; var checkphone = /(\d{3})-(\d{4})/; var checkname = /[a-zA-Z]+(\.|,|\s)*/; var checkzip = /(^967|^968)\d{2}(-\d{4})?$/; // check to see if user have selected an Election if (!document.f1.elections[0].checked && !document.f1.elections[1].checked && !document.f1.elections[2].checked && !document.f1.elections[3].checked) { window.alert("Please select an Election!") return false; // check to see if user entered a valid SSN } else if ( checkssn.test(document.f1.ssn.value) == false){ window.alert("[1]. Please enter a valid social security number in the format ddd-dd-ddd"); return false; // check to see if user entered a valid home telephone number or business telephone number }else if ( document.f1.home_phone.value == '' && document.f1.business_phone.value == '') { window.alert("[4]. Please enter a Home or Business telephone number!") return false; } else if ( document.f1.home_phone.value != ''){ if (checkphone.test(document.f1.home_phone.value) == false){ window.alert("[4]. Please enter a valid home phone number in the format ddd-ddd"); return false; } } else if ( document.f1.business_phone.value != ''){ if ( checkphone.test(document.f1.business_phone.value) == false){ window.alert("[4]. Please enter a valid business phone number in the format ddd-ddd"); return false; } // check to see if user entered a valid Name }else if ( checkname.test(document.f1.lastname.value) == false){ window.alert("[5]. Last Name can only consist of letters, periods(.), commas(,) and spaces"); return false; }else if ( checkname.test(document.f1.firstname.value) == false){ window.alert("[5]. First Name can only consist of letters, periods(.), commas(,) and spaces"); return false; The problem occurs when it validates the phone numbers. When a valid number is entered, it will not move to the next else-if statement to validate the name. It's been years since I program in Java/C, so I'm a bit rusty. Any help is appreciated. -Alex Here is the code, in full: <htmll"> <head><title></title> <script type="text/javascript"> <!-- HIDE FROM INCOMPATIBLE BROWSERS function checkGrade(grade, value) { switch (grade) { case "A": if(grade == '90') window.alert("Your grade is excellent."); break; case "B": window.alert("Your grade is good."); break; case "C": window.alert("Your grade is fair."); break; case "D": window.alert("You are barely passing."); break; case "F": window.alert("You failed."); break; } } // STOP HIDING FROM INCOMPATIBLE BROWSERS --> </script> </head> <body> <form name="gradeForm"action="Your Grades"> <input type="text" name="grade" /> <input type="button" value="Check Grade" onclick="checkGrade(document.gradeForm.grade.value);" /> </form> </body> </html> What's throwing me off is the "A" in the case. As well, "(document.gradeForm.grade.value)" I need to make it a IF statement, but not sure how to call that function. edit:: got it to work setting inBooth to false in the function where I clear the menu, but then it's refreshing to div so often while you're standing in the booth (30 frames/sec, it's a game) that nothing happens when you click the buttons to buy/sell unless you click really really fast.... anyone know a good way to check whether something is true, then toggle an innerHTML div, then once the value becomes false, toggle the innerHTML div to empty....??simply?? But if I reverse the order the opposite works but then the other doesn't...... Code: if (condition==true ) { generateSale(); inBooth=true; smithShop(); } else {inBooth=false;clearSale();} //IS PLAYER IN VENDOR BOOTH? if (condition2==true ) { generateSale(); inBooth=true; vendorShop(); } else {inBooth=false;clearSale();} Only the "generateSale()" from the second one executes. If I switch the if blocks, the second one with "generateSale()" executes. If I change "clearSale();" to a "setTimeout=('clearSale()',1000)" then both execute "generateSale()" but the div it affects flickers repeatedly as it's cleared/remade. generateSale() changes the innerHTML of a div, clearSale() just overwrites the innerHTML with ' ' Code: var sales2; function clearSale() { sales2=document.getElementById('sales2'); sales2.innerHTML=''; } function generateSale(){ sales2=document.getElementById('sales2'); sales2.innerHTML= 'Buy/Sell: '+' Gold coins: '+playerInventory[lootArray[7]]+'<br>' +' Consumables:<br>' +'You have: '+playerItems[itemArray[4]] +' arrows<br>' +'<input type="button" value="Buy" onclick="buyIt(0);">'+'10 for 50gp<br>' +'<input type="button" value="Sell" onclick="sellIt(0);">'+'10 for 25gp<br>' } Any ideas? Basically when my character enters the range of an image of a building I have a menu pop up saying you're in a vendor booth. I want it to run generateSale() which changes a div on the screen to list a bunch of stuff you can buy/sell. When your character is no longer standing in range, I want it to clear that information, so you have to be standing within range in order to trade items. Currently I can make it generates the sale menu, but it doesn't clear it when you leave the range. I've tried all kinds of "toggle" variables to set whether you're in range or not, but only the second block fires, not the first and I cannot figure out why... Any ideas? The code all works fine it just won't clear for all the different buildings, only one.... Code: <html> <script> // Author: L Freeman // Date: 11/10/10 // Purpose: Calculate student discount/vat reductions var price = 0; var vat = 17.5; var discount = 10; var reduction = 0; var deduction = 0; var reductionVAT = 0; var priceDiscount = 0; var priceNoVATDiscount = 0; // Calculate new price price = prompt("Enter price:"); // Calculate reduction reduction = (price/100) * discount; priceDiscount = (reductionVAT-reduction); priceNoVATDiscount = (price - reduction); // Calculate vat deduction deduction = (price/100) * vat; reductionVAT = (price - deduction); // Is VAT and/or a discount required? var answer = confirm ("VAT required?") if (answer) { alert ("Price = " + price + ", VAT = " + deduction + ", New Price is: " + reductionVAT) var answer = confirm ("Discount Required?") if (answer) { alert ("Price = " + reductionVAT + ", Discount = " + reduction + "New price is: " + priceDiscount) else alert ("No Discount Required. Price is: " + reductionVAT) } else alert ("No VAT required.") var answer = confirm ("Discount Required?") { if (answer) alert ("Price = " + price + ", Discount = " + reduction + "New price is: " + priceNoVATDiscount) else alert ("No Discount Required. Price is: " + price) } } </script> </html> I am having trouble getting this to work properly. What I am trying to do is come up with a simple vat deduction + student discount calculator. I cannot seem to get it working. Okay, so I got this code originally from another site. (A few months ago) I don't remember the site, and I was wondering if anyone could help me get it back how it was. Originally I believe the if statements were like this: Code: if (hr == 6, 7, 8) document.write("") I want the same code to appear when any of these numbers appear. Here is my current code: Code: document.write("<center><font size=+1>") day = new Date() hr = day.getMinutes() document.write("") if (hr == 0) document.write("<script type='text/javascript' src='http://www.1800banners.com/adserver/adserver.js'></script> <script type='text/javascript'> var varsarray=[]; varsarray[0]='24047'; if(!token) {var token='0'} else {var token=token+1;} var loadtype='0'; var adtype='0'; var background_color='#9C9C9C'; var border_color='#9C9C9C'; var text_color='#060606'; var text_rollcolor='#000000'; var domain_color='#000000'; varsarray['interhours']=''; varsarray['interpages']=''; adserver_initialize(token,loadtype,adtype,background_color,border_color,text_color,text_rollcolor,domain_color,varsarray); </script>") if (hr == 1) document.write("") if (hr == 2) document.write("") if (hr == 3) document.write("") if (hr == 4) document.write("") if (hr == 5) document.write(") if (hr == 6) document.write("") if (hr == 7) document.write("") if (hr == 8) document.write("") if (hr == 9) document.write("") if (hr == 10) document.write("") if (hr == 11) document.write("") if (hr == 12) document.write("") if (hr == 13) document.write("") if (hr == 14) document.write("") if (hr == 15) document.write("") if (hr == 16) document.write("") if (hr == 17) document.write("") if (hr == 18) document.write("") if (hr == 19) document.write("") if (hr == 20) document.write("") if (hr == 21) document.write("") if (hr == 22) document.write("") if (hr == 23) document.write("") if (hr == 24) document.write("") if (hr == 25) document.write("") if (hr == 26) document.write("") if (hr == 27) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 28) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 29) document.write("") if (hr == 30) document.write("<script type='text/javascript' src='http://www.1800banners.com/adserver/adserver.js'></script> <script type='text/javascript'> var varsarray=[]; varsarray[0]='24047'; if(!token) {var token='0'} else {var token=token+1;} var loadtype='0'; var adtype='0'; var background_color='#9C9C9C'; var border_color='#9C9C9C'; var text_color='#060606'; var text_rollcolor='#000000'; var domain_color='#000000'; varsarray['interhours']=''; varsarray['interpages']=''; adserver_initialize(token,loadtype,adtype,background_color,border_color,text_color,text_rollcolor,domain_color,varsarray); </script>") if (hr == 31) document.write("") if (hr == 32) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 33) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 34) document.write("") if (hr == 35) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=7&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 36) document.write("") if (hr == 37) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 38) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 39) document.write("") if (hr == 40) document.write("<script type='text/javascript' src='http://adhitzads.com/138517'></script>") if (hr == 41) document.write("") if (hr == 42) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 43) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 44) document.write("") if (hr == 45) document.write("<script type='text/javascript' src='http://adhitzads.com/136216'></script>") if (hr == 46) document.write("") if (hr == 47) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 48) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 49) document.write("") if (hr == 50) document.write("<SCRIPT LANGUAGE='JavaScript1.1' SRC='http://bdv.bidvertiser.com/BidVertiser.dbm?pid=244169&bid=856524' type='text/javascript'></SCRIPT>") if (hr == 51) document.write("") if (hr == 52) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 53) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 54) document.write("") if (hr == 55) document.write("<form action='https://www.paypal.com/cgi-bin/webscr' method='post'><input type='hidden' name='cmd' value='_s-xclick'> <input type='hidden' name='hosted_button_id' value='8220588'> <input type='image' src='https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'> <img alt='' border='0' src='https://www.paypal.com/en_US/i/scr/pixel.gif' width='1' height='1'> </form>") if (hr == 56) document.write("") if (hr == 57) document.write("") if (hr == 58) document.write("") if (hr == 59) document.write("") document.write("</font></center>") Any help? Or do you need more information? Hi, been using javascript for a while now, and wondering how else this could be coded to be more effective and to 'work'. The idea is that the user gets prompted, 'what is your birthday?' the user would put in 0121 or whatever their birthday is, as 01 = month and 21 = day. I'm sure you'll understand what I mean.... Code: <DOCTYPE html> <html> <script> var userAnswer = prompt("When is your birthday? Example: Jan 21st = 0121") </script> <head> </head> <body> <script> if(userAnswer >= 0101){ alert('You are Capricorn!') }; if(userAnswer >= 0120){ alert('You are Aquarius!') }; if(userAnswer >= 0219){ alert('You are Pisces!') }; if(userAnswer >= 0321){ alert('You are Aries') }; if(userAnswer >= 0420){ alert('You are Taurus') }; if(userAnswer >= 0521){ alert('You are Gemini!') }; if(userAnswer >= 0621){ alert('You are Cancer!') }; if(userAnswer >= 0723){ alert('You are Leo!') }; if(userAnswer >= 0823){ alert('You are Virgo!') }; if(userAnswer >= 0923){ alert('You are Libra!') }; if(userAnswer >= 1023){ alert('You are Scorpio!') }; if(userAnswer >= 1122){ alert('You are Sagittarius!') }; if(userAnswer >= 1222){ alert('You are Capricorn!') }; </script> </body> </html> Reply With Quote 01-18-2015, 10:01 AM #2 Mitchellwood101 View Profile View Forum Posts New to the CF scene Join Date Nov 2014 Posts 7 Thanks 0 Thanked 0 Times in 0 Posts And then it will alert the user of their star sign****** Reply With Quote 01-18-2015, 05:47 PM #3 Philip M View Profile View Forum Posts Supreme Master coder! Join Date Jun 2002 Location London, England Posts 18,371 Thanks 204 Thanked 2,573 Times in 2,551 Posts If you have been using Javascript for a while now, you should be aware that prompts and alerts are long obsolete, and I would advise that you should not be wasting your time learning this stuff. Use DOM methods to obtain input from and display messages to your users. Be aware that a value starting with 0 is a string, not a number, and so must be in quotes - that is why your code does not work. In any case you need rigorous checking to ensure that the birthday entered in this form is valid, e.g. not 56th Febtember or "Mickey Mouse". You would do best to obtain the input from select lists for month and day, expressed as numbers. A tiny bit more sophistication would allow the user to select a month by name, where the text of the option is the month name, and the value of that option the month number. Code: <select id = "monthlist" onchange = "getSignFunction()"> <option value = "0"> Choose your birth month</option> <option value = 1>January</option> <option value = 2>February</option> ... and so on </select> Then you want something like this:- Code: function getSignFunction()"{ var sign = ""; var month = document.getElementById("monthlist").value; var date = document.getElementById("datelist").value; if (month == 0) {return} // invalid choice if (month == 1 && date >=20 || month == 2 && date <=18) {sign = "Aquarius";} if (month == 2 && date >=19 || month == 3 && date <=20) {sign = "Pisces";} if (month == 3 && date >=21 || month == 4 && date <=19) {sign = "Aries";} if (month == 4 && date >=20 || month == 5 && date <=20) {sign = "Taurus";} if (month == 5 && date >=21 || month == 6 && date <=21) {sign = "Gemini";} if (month == 6 && date >=22 || month == 7 && date <=22) {sign = "Cancer";} if (month == 7 && date >=23 || month == 8 && date <=22) {sign = "Leo";} if (month == 8 && date >=23 || month == 9 && date <=22) {sign = "Virgo";} if (month == 9 && date >=23 || month == 10 && date <=22) {sign = "Libra";} if (month == 10 && date >=23 || month == 11 && date <=21) {sign = "Scorpio";} if (month == 11 && date >=22 || month == 12 && date <=21) {sign = "Sagittarius";} if (month == 12 && date >=22 || month == 1 && date <=19) {sign = "Capricorn";} alert (sign); // alert used here just for testing purposes. Crate as <span> and then use document.getElementById("spanid").innerHTML = sign to display the result to the user. } All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit. Give me an example for switch statements. I had to create one that tests value of parameter using a text string, I came up with this value Code: function createBar(partyType,percent){ switch(partyType) { case D: <td class='dem'></td> break; Should I use " or ' between the dem? I am using break statements. do all breaks should end with string or do i need the bracket? I hope this does not seem like i getting people to do homework for me again,, just want to verify if i did this area correctly? |