JavaScript - What Have I Done Wrong With This Piece Of Javascript?
I'd be grateful if someone can help me fix this peice of JavaScript code...
function gotoURL(){ var newURL = document.GotoForm.theURL.value if (newURL == "100BC" || newURL == "480BC" || newURL == "1642" || newURL == "1679" || newURL == "1752" || newURL == "future") { window.location.href = "date/" + newURL + ".php"; } else if (newURL == "100bc" || newURL == "480bc") { newURL.replace(/bc/g/, "BC"); } else if (newURL == "FUTURE") { newURL.replace(/FUTURE/g/, "future"); } else{ alert("You entered an invalid year, try inputting another year"); }} Thanks. Similar TutorialsOk, the code below works ok in Firefox but not in IE and I can't figure out why. Please note that when I include the movie src in the body tag, the way it is in the code below, the movie loads fine but the window does not resize as it should (see the document.getElementByID). When I take the movie src out of the body tag however, it doesn't load. It's supposed to load just from the javascript meaning the src shouldn't need to be in the body for this to work. Can anyone see what the problem is? Code: <html> <head> <style type="text/css"> <!-- .fontStyle { font-family: Verdana, Geneva, sans-serif; font-size: small; font-weight: bold; color: #67A2DC; } --> </style> <script type="text/javascript"> /*window.onload=function(){ loadVideo(); } /* function wait() { //loadVideo(); setTimeOut("loadVideo()", 2000); } */ function loadVideo() { <!-- if (parseInt(navigator.appVersion)>3) { if (navigator.appName == "Netscape") { winW = window.innerWidth; winH = window.innerHeight; } if (navigator.appName.indexOf("Microsoft") != -1) { winW = document.body.offsetWidth; winH = document.body.offsetHeight; } } var dimW = winW-50; var dimH = winH-20; var w1 = dimW.toString(); var h1 = dimH.toString(); if(document.getElementById("vidEmbed") != null) { document.getElementById("vidEmbed").src = "oracleWelcome_1.wmv"; document.getElementById("vidEmbed").style.width = w1; document.getElementById("vidEmbed").style.height = h1; } else if(document.getElementById("Player") != null) { document.getElementById("Player").style.width = w1; document.getElementById("Player").style.height = h1; document.getElementById("objectSrc").value = "oracleWelcome_1.wmv"; } } </script> </head> <body><div> <p><span class="fontStyle">Introduction<br> </span> <object id="Player" width="100%" height="100%" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" style="border:0px;"> <param name="autoStart" value="True"> <param name="uiMode" value="full"> <param name="volume" value="50"> <param name="mute" value="false"> <param name="URL" id="objectSrc" value=""> <embed src="" id="vidEmbed" width="100%" height="100%" autostart="true" uimode="full" volume="50" mute="false"> </embed> </object> </p><body onload="loadVideo();"></body> </div> </body> </html> I am trying to validate the form on my site using an external js file. Here is the HTML for my form: <form method="post" form action="sendmail.asp"> <p><label for="emailAddr">Email Address: <input id="emailAddr" name="emailAddr" type="text" size="30" class="reqd email" /> </label></p> <p><span id="spryselect1"> <label for="Players">Players: <select id="player" class="reqd" name="player"> <option value="" selected="selected">Choose a Player</option> <option value="Woodson">Charles Woodson</option> <option value="Rodgers">Aaron Rodgers</option> <option value="Driver">Donald Driver</option> <option value="Hawk">A.J. Hawk</option> <option value="Barnett">Nick Barnett</option> <option value="Bigby">Atari Bigby</option> </select> </label> <span class="selectRequiredMsg">Please select an item.</span></span></p> <p><label for"options"> Size(default is X-Large): <label for="medium"><input type="checkbox" name="options" id="medium" value="Medium" /> X-Large</label> <label for="large"><input type="checkbox" name="options" id="large" value="Large" /> Large</label> </p> <p> <label for="setStyle">Jersy Style: <input type="radio" id="homeStyle" name="setStyle" value="homeStyle" class="radio" /> Home <input type="radio" id="awayStyle" name="setStyle" value="awayStyle" class="radio" /> Away </label></p> <p> <label for="zip"> Zip: <input id="zip" name="zip" type="text" size="10" maxlength="5" class="isZip dealerList" /> </label></p> <p><input type="submit" value="Submit" /> <input type="reset" /></p> <p> </p> <p> </p> </form> Here is the JavaScript file attached: window.onload = initForms; function initForms() { for (var i=0; i< document.forms.length; i++) { document.forms[i].onsubmit = function() {return validForm();} } document.getElementById("medium").onclick = sizeSet; } function validForm() { var allGood = true; var allTags = document.getElementsByTagName("*"); for (var i=0; i<allTags.length; i++) { if (!validTag(allTags[i])) { allGood = false; } } return allGood; function validTag(thisTag) { var outClass = ""; var allClasses = thisTag.className.split(" "); for (var j=0; j<allClasses.length; j++) { outClass += validBasedOnClass(allClasses[j]) + " "; } thisTag.className = outClass; if (outClass.indexOf("invalid") > -1) { invalidLabel(thisTag.parentNode); thisTag.focus(); if (thisTag.nodeName == "INPUT") { thisTag.select(); } return false; } return true; function validBasedOnClass(thisClass) { var classBack = ""; switch(thisClass) { case "": case "invalid": break; case "reqd": if (allGood && thisTag.value == "") { classBack = "invalid "; } classBack += thisClass; break; case "radio": if (allGood && !radioPicked(thisTag.name)) { classBack = "invalid "; } classBack += thisClass; break; case "isNum": if (allGood && !isNum(thisTag.value)) { classBack = "invalid "; } classBack += thisClass; break; case "isZip": if (allGood && !isZip(thisTag.value)) { classBack = "invalid "; } classBack += thisClass; break; case "email": if (allGood && !validEmail(thisTag.value)) { classBack = "invalid "; } classBack += thisClass; break; default: if (allGood && !crossCheck(thisTag,thisClass)) { classBack = "invalid "; } classBack += thisClass; } return classBack; } function crossCheck(inTag,otherFieldID) { if (!document.getElementById(otherFieldID)) { return false; } return (inTag.value != "" || document.getElementById(otherFieldID).value != ""); } function radioPicked(radioName) { var radioSet = ""; for (var k=0; k<document.forms.length; k++) { if (!radioSet) { radioSet = document.forms[k][radioName]; } } if (!radioSet) { return false; } for (k=0; k<radioSet.length; k++) { if (radioSet[k].checked) { return true; } } return false; } function isNum(passedVal) { if (passedVal == "") { return false; } for (var k=0; k<passedVal.length; k++) { if (passedVal.charAt(k) < "0") { return false; } if (passedVal.charAt(k) > "9") { return false; } } return true; } function isZip(inZip) { if (inZip == "") { return true; } return (isNum(inZip)); } function validEmail(email) { var invalidChars = " /:,;"; if (email == "") { return false; } for (var k=0; k<invalidChars.length; k++) { var badChar = invalidChars.charAt(k); if (email.indexOf(badChar) > -1) { return false; } } var atPos = email.indexOf("@",1); if (atPos == -1) { return false; } if (email.indexOf("@",atPos+1) != -1) { return false; } var periodPos = email.indexOf(".",atPos); if (periodPos == -1) { return false; } if (periodPos+3 > email.length) { return false; } return true; } function invalidLabel(parentTag) { if (parentTag.nodeName == "LABEL") { parentTag.className += " invalid"; } } } } function sizeSet() { if (this.checked) { document.getElementById("homeStyle").checked = true; } } Thank you very much in advance for any and all help. I am a noob with JavaScript. I didn't know where else to turn for help. Thanks in advance, guys. The URL of the page I'm working on can be found he http://graph-art.matc.edu/romanot/vi...inal/order.php Hello, I have a very simple file upload form, which I'm submitting with javascript whenever the value in the file input changes. I open the Open File.. dialog with javascript too(in IE - in FF the control just has opacity:0 over a button), just by calling click() on it. This works like a charm in both firefox and IE so far, but when I set either the visibility or z-index of the file input control so that it gets hidden, the form doesn't submit in IE(still works fine in FF). The alert fires, so the function is still called, but submitting just doesn't work - nothing received serverside. Anyone know what I'm doing wrong here/what the fix is? Thanks a lot in advance for any help The form looks like this: Code: <form id="form2" runat="server" name="xmluploadform"> <asp:FileUpload ID="xmlupload" onchange="SubmitForm();" runat="server" /> </form> And the SubmitForm function looks like this: Code: function SubmitForm() { alert('submitting'); $('#form2').submit(); } PS. I thought about maybe it was because the browse... button gets hidden so it can't click it, but then is there any way to set the width of it or something? The width of <input type="file"/> is completely different in IE/FF, so it's impossible to make it fit into the design Hello chaps, I'm Nick I'm in the process of creating a web app using Google Maps. What the following code should do is retrieve some xml code from a page, then load it on to the map, using the geocoder to get lat/long values from the address. Most of it's just Google code I've mauled about with. The map loads, but none of the markers load on to the map. I've gone to the php page which generates the XML for the markers and all is fine there. I know the code is a messy crock of junk, I was sort of concentrating on trying to make it work then I'll sort out the nomenclature. Bad methodology, I know. Code: var id = null; var adress = null; var image = null; var point = null; var pointer = null; var geocoder = null; var iconBlue = new GIcon(); iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png'; iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'; iconBlue.iconSize = new GSize(12, 20); iconBlue.shadowSize = new GSize(22, 20); iconBlue.iconAnchor = new GPoint(6, 20); iconBlue.infoWindowAnchor = new GPoint(5, 1); var iconRed = new GIcon(); iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png'; iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'; iconRed.iconSize = new GSize(12, 20); iconRed.shadowSize = new GSize(22, 20); iconRed.iconAnchor = new GPoint(6, 20); iconRed.infoWindowAnchor = new GPoint(5, 1); var customIcons = []; customIcons["restaurant"] = iconBlue; customIcons["bar"] = iconRed; function load() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById("map")); map.addControl(new GSmallMapControl()); map.addControl(new GMapTypeControl()); map.setCenter(new GLatLng(52.2725, -0.8825), 12); geocoder = new GClientGeocoder(); GDownloadUrl("phpsqlajax_genxml3.php", function(data) { var xml = GXml.parse(data); var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { id = markers[i].getAttribute("ID"); address = markers[i].getAttribute("ADDRESS"); image = markers[i].getAttribute("IMAGE"); var point3 = showAddress(address); var marker = createMarker(pointer, id, address, image); map.addOverlay(marker); } }); } } function showAddress(address) { point = geocoder.getLatLng(address,function(point) { if (!point) { alert(address + " not found"); } else { pointer = new GLatLng(point); return pointer; } }); } function createMarker(pointer, id, address, image) { var marker4 = new GMarker(pointer); var html = "<b>" + id + "</b> <br/>" + address + "</b> <br/>" + image; GEvent.addListener(marker, 'click', function() { marker4.openInfoWindowHtml(html); }); return marker4; } Thanks in advance I am using the same java script to help add shipping costs for paypal in my html. I have worked very long and hard to get to this point. Since everything is very similar and having conflicts I have saved and named each java script for it's form. However, when there are more then one on the html page it always calculates the last form giving me the wrong price per quantity on the first. I have not found a way to differentiate or id and separate. <script src="nineEnvelope.js" type="text/javascript"> </script> <form rel="nofollow" target="paypal" style="position:absolute; left:500px;top:200px; width:400px; height:25px; font-family:helvetica; color:#000000;font-size:18px;" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="business" value=""> <table> <tr><td> <input type="hidden" name="on0" value="#9 Envelope">#9 Envelope <input type="hidden" name="amount" value=""> <input type="hidden" name="shipping" value=""> <input type="hidden" name="shipping2" value=""> <input type="hidden" name="item_number" value=""> <input type="hidden" name="no_shipping" value="2"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="add" value="1"> </td></tr><tr><td> <select name="os0"> <option value="250 qty">250 qty $95.00</option> <option value="500 qty">500 qty $125.00</option> <option value="1,000 qty">1,000 qty $200.00</option> <option value="2,500 qty">2,500 qty $450.00</option> <option value="5,000 qty">5,000 qty $638.00</option> </select> </td></tr> <!-- <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="option_select0" value="250 qty" /> <input type="hidden" name="option_amount0" value="95.00" /> <input type="hidden" name="option_select1" value="500 qty" /> <input type="hidden" name="option_amount1" value="125.00" /> <input type="hidden" name="option_select2" value="1,000 qty" /> <input type="hidden" name="option_amount2" value="200.00" /> <input type="hidden" name="option_select3" value="2,500 qty" /> <input type="hidden" name="option_amount3" value="450.00" /> <input type="hidden" name="option_select4" value="5,000 qty" /> <input type="hidden" name="option_amount4" value="638.00" /> <input type="hidden" name="option_index" value="0" /> --> <tr> <td> <input type="hidden" name="on1" value="window option">window option</td></tr><tr><td><select name="os1"> <option value="no window">no window </option> <option value="window (right)">window (right) </option> <option value="window (left)">window (left) </option> <option value="double window (left)">double window (left) </option> </select> </td></tr> </table> <input onclick=CalculateOrder(this.form) type="image" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" <img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"> </form> </div> </body> </html> <html> <head> <div> <script type="text/javascript" src="nine_envelope_sec_src.js"> </script> <form rel="nofollow" target="paypal" style="position:absolute; left:700px;top:200px; width:400px; height:25px; font-family:helvetica; color:#000000;font-size:18px;" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="business" value=""> <table> <tr><td> <input type="hidden" name="on0" value="#9 Envelope w/ Security Tint">#9 Envelope w/ Security Tint <input type="hidden" name="amount" value=""> <input type="hidden" name="shipping" value=""> <input type="hidden" name="shipping2" value=""> <input type="hidden" name="item_number" value=""> <input type="hidden" name="no_shipping" value="2"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="add" value="1"> </td></tr><tr><td> <select name="os0"> <option value="250 qty">250 qty $110.00</option> <option value="500 qty">500 qty $140.00</option> <option value="1,000 qty">1,000 qty $215.00</option> <option value="2,500 qty">2,500 qty $475.00</option> <option value="5,000 qty">5,000 qty $678.00</option> </select> </td></tr> <!-- <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="option_select0" value="250 qty" /> <input type="hidden" name="option_amount0" value="110.00" /> <input type="hidden" name="option_select1" value="500 qty" /> <input type="hidden" name="option_amount1" value="140.00" /> <input type="hidden" name="option_select2" value="1,000 qty" /> <input type="hidden" name="option_amount2" value="215.00" /> <input type="hidden" name="option_select3" value="2,500 qty" /> <input type="hidden" name="option_amount3" value="475.00" /> <input type="hidden" name="option_select4" value="5,000 qty" /> <input type="hidden" name="option_amount4" value="678.00" /> <input type="hidden" name="option_index" value="0" /> --> <tr> <td><input type="hidden" name="on1" value="window option">window option</td></tr><tr><td><select name="os1"> <option value="no window">no window </option> <option value="window (right)">window (right) </option> <option value="window (left)">window (left) </option> <option value="double window (left)">double window (left) </option> </select> </td></tr> <!-- <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="option_select0" value="no window" /> <input type="hidden" name="option_amount0" value="0" /> <input type="hidden" name="option_select1" value="window (right)" /> <input type="hidden" name="option_amount1" value="0" /> <input type="hidden" name="option_select2" value="window (left)" /> <input type="hidden" name="option_amount2" value="0" /> <input type="hidden" name="option_select3" value="double window (left)" /> <input type="hidden" name="option_amount3" value="0" /> --> </table> <input onclick=CalculateOrder(this.form) type="image" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" <img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"> </form> </div> </head> </html> nineEnvelope.js function CalculateOrder(form) { if (form.os0.value == "250 qty") { form.amount.value = 95.00; form.item_number.value = ""; form.shipping.value = "15.00"; form.shipping2.value = "15.00"; } if (form.os0.value == "500 qty") { form.amount.value = 125.00; form.item_number.value = ""; form.shipping.value = "15.00"; form.shipping2.value = "15.00"; } if (form.os0.value == "1,000 qty") { form.amount.value = 200.00; form.item_number.value = ""; form.shipping.value = "15.00"; form.shipping2.value = "15.00"; } if (form.os0.value == "2,500 qty") { form.amount.value = 450.00; form.item_number.value = ""; form.shipping.value = "20.00"; form.shipping2.value = "20.00"; } if (form.os0.value == "5,000 qty") { form.amount.value = 638.00; form.item_number.value = ""; form.shipping.value = "35.00"; form.shipping2.value = "35.00";} } nine_envelope_sec_src.js function CalculateOrder(form) { if (form.os0.value == "250 qty") { form.amount.value = 110.00; form.item_number.value = ""; form.shipping.value = "15.00"; form.shipping2.value = "15.00"; } if (form.os0.value == "500 qty") { form.amount.value = 140.00; form.item_number.value = ""; form.shipping.value = "15.00"; form.shipping2.value = "15.00"; } if (form.os0.value == "1,000 qty") { form.amount.value = 215.00; form.item_number.value = ""; form.shipping.value = "15.00"; form.shipping2.value = "15.00"; } if (form.os0.value == "2,500 qty") { form.amount.value = 475.00; form.item_number.value = ""; form.shipping.value = "20.00"; form.shipping2.value = "20.00"; } if (form.os0.value == "5,000 qty") { form.amount.value = 678.00; form.item_number.value = ""; form.shipping.value = "35.00"; form.shipping2.value = "35.00"; } } Here's my HTML: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>The Happy Hoppin' Hotel</title> <script src="happyhoppin.js" language="javascript" type="text/javascript"></script> </head> <body> <h1>The Happy Hoppin' Hotel Checkout Page</h1> <h2>Fill out the form below to calculate balance due</h2> <form> Guest ID Number: <input type="text" id="guestID" /> <br /> <br /> Room Type: <select id="roomType"> <option></option> <option>Parlor</option> <option>Single</option> <option>Double</option> </select> <br /> <br /> Length of Stay: <input type="text" id="stayLength" /> <br /> <br /> Number of Drinks: <input type="text" id="drinkNumber" /> <br /> <br /> Number of Towels: <input type="text" id="towelNumber" /> <br /> <br /> Number of Flushes: <input type="text" id="flushNumber" /> <br /> <br /> Bug Complaints?: <br /> <form name="bugComplaintRadio"> <input type="radio" name="bugComplaint" value="No" />No</label> <br /> <input type="radio" name="bugComplaint" value="Yes" />Yes</label> <br /> </form> <br /> Customer Comments: <br /> <textarea name="customerComment" cols="50" rows="5">Enter your comments here...</textarea> <br /> <br /> <input type="button" onclick="calculateBill()" value="Calculate Bill"> </form> </body> </html> Here's my Javascript: Code: const parlorPrice = 80; const singlePrice = 100; const doublePrice = 150; const drinkPrice = 5; const towelPrice = 3; const flushPrice = 1; var guestID = 0; var roomPrice = 0; var stayLength = 0; var drinkNumber = 0; var towelNumber = 0; var flushNumber = 0; var totalDue = 0; var totalCharge = 0; function calculateBill(){ validateForm(); //roomType// if(roomType == "Parlor"){ roomPrice = parlorPrice; } if(roomType == "Single"){ roomPrice = singlePrice; } if(roomType == "Double"){ roomPrice = doublePrice; } //roomType// //drinkCharge// drinkCharge = drinkNumber * drinkPrice; //drinkCharge// //towelCharge// towelCharge = towelNumber * towelPrice; //towelCharge// //flushCharge// flushCharge = flushNumber * flushPrice; //flushCharge// //totalCharge// totalCharge = roomPrice + drinkCharge + towelCharge + flushCharge; //totalCharge// //**bugDiscount**// function getCheckedRadio() { bugValue = ""; bugLength = document.bugComplaintRadio.bugComplaint.length; var bugDiscount = 0; for (x = 0; x < bugLength; x ++) { if (document.bugComplaintRadio.bugComplaint[x].checked) { bugValue = document.bugComplaintRadio.bugComplaint[x].value; } } if (bugValue == "") { alert("You did not choose whether you had a bug complaint or not"); } if (bugValue = "No"){ bugDiscount = 0; } if (bugValue = "Yes"){ bugDiscount = 20; } } //**bugDiscount**// getCheckedRadio(); //totalDue// totalDue = totalCharge + bugDiscount //totalDue// displayBill(); } function validateForm(){ //guestID// guestID = parseInt(document.getElementById("guestID").value); if(isNaN(guestID)){ alert("Guest ID must be a number"); return; } if(guestID <= 0){ alert("Guest ID must be greater than zero"); return; } //guestID// //roomType// roomType = document.getElementById("roomType").value; if(roomType == ""){ alert("Room type must be selected"); return; } //roomType// //stayLength// stayLength = parseInt(document.getElementById("stayLength").value); if(isNaN(stayLength)){ alert("Length of stay must be a number"); return; } if(stayLength <= 0){ alert("Length of stay must be greater than zero"); return; } //stayLength// //drinkNumber// drinkNumber = parseInt(document.getElementById("drinkNumber").value); if(isNaN(drinkNumber)){ alert("Number of drinks must be a number"); return; } if(drinkNumber <= 0){ alert("Number of drinks must be greater than zero"); return; } if(drinkNumber > 25){ alert("Number of drinks has exceeded 25"); return; } //drinkNumber// //towelNumber// towelNumber = parseInt(document.getElementById("towelNumber").value); if(isNaN(towelNumber)){ alert("Number of towels must be a number"); return; } if(towelNumber <= 0){ alert("Number of towels must be greater than zero"); return; } //towelNumber// //flushNumber// flushNumber = parseInt(document.getElementById("flushNumber").value); if(isNaN(flushNumber)){ alert("Number of flushes must be a number"); return; } if(flushNumber <= 0){ alert("Number of flushes must be greater than zero"); return; } //flushNumber// //customerComment// customerComment = document.getElementById("customerComment"); //customerComment// } function displayBill(){ var newPage = "<html><head><title>Billing Summary</title></head>"; newPage += "<body><h1>Happy Hoppin Hotel</h1>"; newPage += "<h2>Guest Billing Statement</h2>"; newPage += "Guest Identification: #" + guestID; newPage += "<br />"; newPage += "Room Type: " + roomType; newPage += "<br />"; newPage += "Room Charge: $" + roomPrice; newPage += "<br />"; newPage += "Length of Stay: " + stayLength + " days"; newPage += "<br />"; newPage += "Drink Charge: $" + drinkCharge; newPage += "<br />"; newPage += "Towel Charge: $" + towelCharge; newPage += "<br />"; newPage += "Flushing Charge: $" + flushCharge; newPage += "<br />"; newPage += "Total Charge: $" + totalCharge; newPage += "<br />"; newPage += "Discount: $" + bugDiscount; newPage += "<br />"; newPage += "Total Due: $" + totalDue; newPage += "<br />"; newPage += "<h3>Come back and visit us again at the Happy Hoppin' Hotel!</h3>"; var z = window.open("","","width=400,height=500"); z.document.write(newPage); z.document.close(); } My question is, I've been spending countless hours trying to: 1. Make two radio buttons indicating "No" and "Yes", 2. Retrieve which selection the user has made, 3. Change the value of "bugDiscount" or the amount of money ($20 or $0) depending on which choice the user made; $0 for No & $20 for Yes. 4. Subtract the value of bugDiscount (0 or 20) from the totalCharge to get TotalDue I know I'm close, but I've tried any number of variations in my code and I still can't seem to get it right. Can anyone help? I have a CSS file associated with this that sets the width at "200px." However, I am getting a 40px wide dropdown. Can anybody catch an error in here? Using console.log, I found that el, as soon as it is defined, is = div.dropdown. As soon as .width comes into the picture, it returns as six consecutive 101px and a ton of 40px after that... and continues returning those values till the end. I appreciate any input!! Code: var Dropdown = function(el) { this.el = el; this.handle = el.find("div.dropdown-text"); this.options = el.find("div.dropdown-options"); this.optionsWrap = this.options.parent(); this.action = el.attr("action"); this.type = el.attr("type"); this.target = el.attr("target"); this.Init(); } Dropdown.prototype.Init = function() { var self = this; var extraPadding = 40; this.isShown = false; console.log(this); this.width = this.options.outerWidth() + extraPadding; this.height = this.options.outerHeight(); if (this.height > 300) { this.height = 300; this.options .height(300) .width(this.width + 12) .addClass("scroll-pane") .jScrollPane({ showArrows: true, scrollbarWidth: 9 }) this.width += 12; } else { this.options.width(this.options.width() + extraPadding); } var newWidth = (this.width < 220) ? this.width : 220; this.el.width(newWidth); this._AddShadow(); this.optionsWrap.css({ display: "none", visibility: "visible", width: this.width + 5, height: this.height + 5 }); this._SetEvents(); }; Dropdown.prototype.Toggle = function(force) { if (this.isShown && !force) { this.optionsWrap.hide(); this.isShown = false; } else if (!this.isShown && false !== force){ this.optionsWrap.show(); this.isShown = true; } } Dropdown.prototype._AddShadow = function() { var shadowWidth = 5, layer; for (var i = 0; i < shadowWidth; i++) { layer = $('<div class="shadow_layer"> </div>'); layer.css({ opacity: 0.055 * (i + 1), top: (shadowWidth - i), left: (shadowWidth - i), height: this.height, width: this.width }); this.optionsWrap.prepend(layer); } } Dropdown.prototype._SetEvents = function() { var self = this; this.el.bind('click', function(e) { var current = $(e.target); var type = current.attr("type"); if (current.hasClass('dropdown-text') || current.hasClass('dropdown-arrow')) { if (!self.isShown) { page.HideAllDropdowns(); } self.Toggle(); return false; } if (current.hasClass('dropdown-opt')) { switch (type) { case 'auto': case 'basic': var html = current.html(); self.handle.html(html); self.options.find('a.dropdown-opt').each(function() { $(this).removeClass('selected'); }); if ('auto' == type) { page.common.Update({ url: current.attr('path'), params: current.attr('params'), target: self.target, parent: true, method: "POST", handler: { handler: function(results) { var action = this.attr("action"); if (action) { page.events.Fire(action, this, results) }; }, context: current } }); } current.addClass('selected'); self.Toggle(); return false; case 'multi': current.toggleClass('selected'); break; case 'function': var html = current.html(); self.handle.html(html); current.Fire(); self.Toggle(false); break } } return false; }); } Here's the basic concept, its somewhat of a counter, and I think javascripts the way to go... Basically I'm looking to generate a running number... x + 11 = print the number The tricky part is, I need it to never stop, somewhat like a timer (example http://www.hashemian.com/tools/javascript-countdown.htm) but not a timer... Think the tally mark on McDonald's 99 billion served.. Any suggestions or ideas? I need to understand this code but i am not able to fully understand it to use it for myself. Anybody can you help please function getScore(form) { var score = 0; var currElt; var currSelection; for (i=0; i<numQues; i++) { currElt = i*numChoi; for (j=0; j<numChoi; j++) { currSelection = form.elements[currElt + j]; if (currSelection.checked) { if (currSelection.value == answers[i]) { score++; break; } } } } score = Math.round(score/numQues*100); // math.round function will return the whole number to the nearest specified value for example 1.9 will show as 2 form.percentage.value = score + "%"; // The score will be displayed in percentage var correctAnswers = ""; for (i=1; i<=numQues; i++) { correctAnswers += i + ". " + answers[i-1] + "\r\n"; } form.solutions.value = correctAnswers; } Any short explanation will be a great help. Hi, below is some code I've found at w3schools site. It demonstrates the use of "onkeypress" event by allowing only non-numerical input. It works great and is mostly clear to me, except for the part I marked in red. Could someone please explain this part to me step by step? It's probably a noobish question, but I thought you could only use "return" inside a function. Are "return true" and "return false" just optional parameters to the "onkeypress" event? If so, where else can they be used? Also what information exactly does the "(event)" being passed into the function "noNumbers()" carry? <html> <body> <script type="text/javascript"> function noNumbers(e) { var keynum; var keychar; var numcheck; if(window.event) // IE8 and earlier { keynum = e.keyCode; } else if(e.which) // IE9/Firefox/Chrome/Opera/Safari { keynum = e.which; } keychar = String.fromCharCode(keynum); numcheck = /\d/; return !numcheck.test(keychar); } </script> <form> Type some text (numbers not allowed): <input type="text" onkeypress= "return noNumbers(event)" /> </form> I have a piece of code that pulls reviews and allows a business owner to drop the reviews on to their website. I need to limit the number of reviews and ideally I'd like to do that with just the snippet the biz owner drops into their site. Is this possible? Here's a sample of the code: <div id="repagent_reviews"></div><script type="text/javascript" src="http://awebsite.com/anotherwebsite/wp-admin/admin-ajax.php?action=repagent_reviews_js&id=1182"></script> thanks! confused Hello! I have a really simple JavaScript calculator I'm running, and for the life of me can not figure out how to solve this crazy number problem. It's not doing the math properly, and javascript is not my strongest suit. All my script does is take user input of numbers into a form field, subtract that from another form and multiply the answer of those two forms by whatever the user put in. Example: 210 (minus) 120 (multiplied by) .90 = 81 Here is the actual script: Code: // Calculator function CalculateSum(Atext, Btext, Ctext, form) { var A = parseFloat(Atext); var B = parseFloat(Btext); var C = parseFloat(Ctext); form.Answer.value = A - B * C; } /* ClearForm: this function has 1 argument: form. It clears the input and answer fields on the form. It needs to know the names of the INPUT elements in order to do this. */ function ClearForm(form) { form.input_A.value = ""; form.input_B.value = ""; form.input_C.value = ""; form.Answer.value = ""; } // end of JavaScript functions --> And the html I am using: Code: <form name="Calculator" method="post"> <p>Base Average<input type=text name="input_A"></p> <p>Current Average:<input type=text name="input_B"></p> <p>Percentage of:<input type=text name="input_C"></p> <p>Your ball speed is: (miles per hour) <input name="Answer" type=text readonly> </p> <p> <input type="button" value="Calculate Handicap" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"> <input type="button" value="Clear" name="ClearButton" onClick="ClearForm(this.form)"> </p> </form> Any help would be greatly appreciated! Hi, I have the below javascript that insert smiles for me. But in Firefox, it is inserting at the bottom of the message Edit: This only happens when there in no text input first before the smile. For exmple Quote: -- Original Message --- test send :-) -- End Original Message --- ;-) I would like it be inserted, like Quote: ;-) -- Original Message --- test send :-) -- End Original Message --- Note: ;-) would be the smile It works fine in Internet Explorer, but Firefox, put it in the wrong place, can anyone help. The javascript code I use is below: Code: //Smile Start var myAgent = navigator.userAgent.toLowerCase(); var myVersion = parseInt(navigator.appVersion); var is_ie = ((myAgent.indexOf("msie") != -1) && (myAgent.indexOf("opera") == -1)); var is_nav = ((myAgent.indexOf('mozilla')!=-1) && (myAgent.indexOf('spoofer')==-1) && (myAgent.indexOf('compatible') == -1) && (myAgent.indexOf('opera')==-1) && (myAgent.indexOf('webtv') ==-1) && (myAgent.indexOf('hotjava')==-1)); var is_win = ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1)); var is_mac = (myAgent.indexOf("mac")!=-1); function smile( txt ) { // document.all.txtmessage.value = document.all.txtmessage.value + txt; // return false; doInsert(" " + txt + " ", "", false,document.getElementById('txtmessage')); } function smile2( txt ) { doInsert(" " + txt + " ", "", false,document.getElementById('txttemplate')); } function smile3( txt ) { doInsert(" " + txt + " ", "", false,document.getElementById('txtcomments')); } function doInsert(ibTag, ibClsTag, isSingle, name_txt) { var isClose = false; var obj_ta = name_txt; //---------------------------------------- // It's IE! //---------------------------------------- if ( (myVersion >= 4) && is_ie && is_win) // if ( (ua_vers >= 4) && is_ie && is_win) { if (obj_ta.isTextEdit) { obj_ta.focus(); var sel = document.selection; var rng = sel.createRange(); rng.colapse; if((sel.type == "Text" || sel.type == "None") && rng != null) { if(ibClsTag != "" && rng.text.length > 0) ibTag += rng.text + ibClsTag; else if(isSingle) isClose = true; rng.text = ibTag; } } else { //-- mod_bbcode begin // this should work with Mozillas if ( (myVersion >= 4) && is_win) { var length = obj_ta.textLength; var start = obj_ta.selectionStart; var end = obj_ta.selectionEnd; if (end == 1 || end == 2) end = length; var head = obj_ta.value.substring(0,start); var rng = obj_ta.value.substring(start, end); var tail = obj_ta.value.substring(end, length); if( start != end ){ if (ibClsTag != "" && length > 0) ibTag += rng + ibClsTag; else if (isSingle) isClose = true; rng = ibTag; obj_ta.value = head + rng + tail; start = start + rng.length; } else{ if(isSingle) isClose = true; obj_ta.value = head + ibTag + tail; start = start + ibTag.length; } obj_ta.selectionStart = start; obj_ta.selectionEnd = start; } else { //-- mod_bbcode end if(isSingle) { isClose = true; } obj_ta.value += ibTag; //-- mod_bbcode begin } //-- mod_bbcode end } } //---------------------------------------- // It's MOZZY! //---------------------------------------- else if ( obj_ta.selectionEnd ) { var ss = obj_ta.selectionStart; var st = obj_ta.scrollTop; var es = obj_ta.selectionEnd; if (es <= 2) { es = obj_ta.textLength; } var start = (obj_ta.value).substring(0, ss); var middle = (obj_ta.value).substring(ss, es); var end = (obj_ta.value).substring(es, obj_ta.textLength); //----------------------------------- // text range? //----------------------------------- if (obj_ta.selectionEnd - obj_ta.selectionStart > 0) { middle = ibTag + middle + ibClsTag; } else { middle = ibTag + middle; if (isSingle) { isClose = true; } } obj_ta.value = start + middle + end; var cpos = ss + (middle.length); obj_ta.selectionStart = cpos; obj_ta.selectionEnd = cpos; obj_ta.scrollTop = st; } //---------------------------------------- // It's CRAPPY! //---------------------------------------- else { if (isSingle) { isClose = true; } obj_ta.value += ibTag; } obj_ta.focus(); return isClose; } function CDE(elemId) { if(document.getElementById(elemId).style.display != "none") document.getElementById(elemId).style.display = "none" else document.getElementById(elemId).style.display = "inline" } //Smile End Thanks for any help you can give me Hi, I have this code: Code: window.addEvent('domready',function(){ document.id('cool').addEvent('click',function() { light = new LightFace.IFrame({ height:400, width:800, url: 'http://google.com', title: 'Google!' }).addButton('Close', function() { light.close(); },true).open(); }); }); It's a piece from something called the lightface iframe. What I want to do is to have this code on my javascript page, that's included on the main page where I want to call it. I call on it like this: Code: <a href="#" id="cool">Edit general info</a> However this only works if it's on the main page, not when I have it on my js page. So what I want to do is to turn it into a function, because that I know works. Could anyone help me with this? Thanks in advance for your time! I don't know anything about Javascript. My brother-in-law wanted me to update some pictures on his website. I found a piece of javascript code and am curious as to what it's function would be. Could someone please tell me what this piece of code is doing / what it's being used for. Thanks in advance. Here's the code: Code: <csscriptdict> <script><!-- function CSClickReturn () { var bAgent = window.navigator.userAgent; var bAppName = window.navigator.appName; if ((bAppName.indexOf("Explorer") >= 0) && (bAgent.indexOf("Mozilla/3") >= 0) && (bAgent.indexOf("Mac") >= 0)) return true; // dont follow link else return false; // dont follow link } CSStopExecution = false; function CSAction(array) { return CSAction2(CSAct, array); } function CSAction2(fct, array) { var result; for (var i=0;i<array.length;i++) { if(CSStopExecution) return false; var actArray = fct[array[i]]; if(actArray == null) return false; var tempArray = new Array; for(var j=1;j<actArray.length;j++) { if((actArray[j] != null) && (typeof(actArray[j]) == "object") && (actArray[j].length == 2)) { if(actArray[j][0] == "VAR") { tempArray[j] = CSStateArray[actArray[j][1]]; } else { if(actArray[j][0] == "ACT") { tempArray[j] = CSAction(new Array(new String(actArray[j][1]))); } else tempArray[j] = actArray[j]; } } else tempArray[j] = actArray[j]; } result = actArray[0](tempArray); } return result; } CSAct = new Object; CSAg = window.navigator.userAgent; CSBVers = parseInt(CSAg.charAt(CSAg.indexOf("/")+1),10); function IsIE() { return CSAg.indexOf("MSIE") > 0;} function CSIEStyl(s) { return document.all.tags("div")[s].style; } function CSNSStyl(s) { return CSFindElement(s,0); } function CSFindElement(n,ly) { if (CSBVers < 4) return document[n]; var curDoc = ly ? ly.document : document; var elem = curDoc[n]; if (!elem) { for (var i=0;i<curDoc.layers.length;i++) { elem = CSFindElement(n,curDoc.layers[i]); if (elem) return elem; }} return elem; } function CSSetImageURL(action) { var img = null; if (document.images) { if (!IsIE()) img = CSFindElement(action[1],0); else img = document.images[action[1]]; if (img) img.src = action[2]; } }function CSGotoLink(action) { if (action[2].length) { var hasFrame=false; for(i=0;i<parent.frames.length;i++) { if (parent.frames[i].name==action[2]) { hasFrame=true; break;}} if (hasFrame==true) parent.frames[action[2]].location = action[1]; else window.open (action[1],action[2],""); } else location = action[1]; } // --></script> </csscriptdict> <csactiondict> <script><!-- CSAct[/*CMP*/ 'B52BCE950'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/geebee.jpg'); CSAct[/*CMP*/ 'B527CA9F3'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/cutline.gif'); CSAct[/*CMP*/ 'B527CAD04'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/f-4.jpg'); CSAct[/*CMP*/ 'B527CAE85'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/cutline.gif'); CSAct[/*CMP*/ 'B527CBCF6'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/boeing-314.jpg'); CSAct[/*CMP*/ 'B527CBEF7'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/cutline.gif'); CSAct[/*CMP*/ 'B527CC588'] = new Array(CSGotoLink,/*URL*/ 'goldenage.htm',''); CSAct[/*CMP*/ 'B527CD869'] = new Array(CSGotoLink,/*URL*/ 'aboutus.html',''); CSAct[/*CMP*/ 'B527D7110'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/HE-111.jpg'); CSAct[/*CMP*/ 'B527D7281'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/cutline.gif'); CSAct[/*CMP*/ 'B527D73B2'] = new Array(CSGotoLink,/*URL*/ 'wwiiidmodels.htm',''); CSAct[/*CMP*/ 'B527D75D3'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/lancaster.jpg'); CSAct[/*CMP*/ 'B527D77E4'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/cutline.gif'); CSAct[/*CMP*/ 'B527D7895'] = new Array(CSGotoLink,/*URL*/ 'wwiiexpert.htm',''); CSAct[/*CMP*/ 'B527D9120'] = new Array(CSGotoLink,/*URL*/ 'modeler.html',''); CSAct[/*CMP*/ 'B527D9291'] = new Array(CSGotoLink,/*URL*/ 'faq.html',''); CSAct[/*CMP*/ 'B527D9372'] = new Array(CSGotoLink,/*URL*/ 'contact.html',''); CSAct[/*CMP*/ 'B527D94A3'] = new Array(CSGotoLink,/*URL*/ 'custom.htm',''); CSAct[/*CMP*/ 'B527D96E5'] = new Array(CSGotoLink,/*URL*/ 'seaplanes.htm',''); CSAct[/*CMP*/ 'B52D133826'] = new Array(CSGotoLink,/*URL*/ 'postwarmodels.htm',''); CSAct[/*CMP*/ 'B530F9380'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/F-104.jpg'); CSAct[/*CMP*/ 'B530F9501'] = new Array(CSSetImageURL,/*CMP*/ 'cac logo',/*URL*/ 'Images/components/cutline.gif'); // --></script> </csactiondict> Here's the website that it's on: http://www.aircraftmodels.com/index.htm Hi, can anyone help me with this? The following html is to upload an image, it works ok on firefox, but while using google chrome, nothing happened when I clicked "upload LOGO" button. .................................................................................................... .... <html> <body> <form method="post" action="action_start_initial_registration.php" name="initial_registration" id="initial_registration" enctype="multipart/form-data"> <input type="file" style="display:none" onchange="checkLogo()" name="input_logo_file" id="input_logo_file" value="input_logo_file"> <input type="text" name="logo_flag" id="logo_flag" value="0" style="display:none;"/> <a href="javascript:void(0);" onclick="input_logo_file.click()"><em>upload LOGO</em></a><br /> </form> <script type="text/javascript"> function checkLogo(){ document.getElementById("logo_flag").value="1"; document.getElementById("initial_registration").submit(); } </script> </body> </html> ............................................................................................. First, I am new to javascript... I've been trying to get help on a dynamic menu but no one ever seems to respond... So I will start with the pieces... I do not understand why this chunk of code is not working... All i have is a div and the script is simply supposed to make the div have a new size... here is the script... Code: <script type="text/javascript"> var stretch = 200; function Expand(index) { var dname = "menu" + index; document.getElementById(dname).style.width = stretch+'px'; } </script> here is the styling Code: <style type="text/css"> .show{height:20px; width:100px; display:block; background:lightgreen;} </style> and here is the simple html Code: <div id="menu1" class="show" onclick="runExpand(1)"></div> first- i wanted to say (for all those who see it before the Mods remove it) how stupid does a bot have to be to set its spam to be resolved at OP? lol so now my question... I have two divs with some styling. Code: #show{CSS code} #menuDet1{CSS code and visibility:hidden;} I then had in html Code: <div id="show" onmouseover="changevis(1)"></div> <div id="me1"></div> and then a function Code: function changevis(index){ acMenuDet="me" + index; document.getElementById(acMenuDet).visibility="visible"; } a colleague of mine helped me "debug" this until it worked and we came up with Code: function details_visible(index) { var acMenuDet = "menuDet" + index; document.getElementById(acMenuDet).style.visibility = "visible"; ; } function details_hidden(index) { var acMenuDet = "menuDet" + index; document.getElementById(acMenuDet).style.visibility = "hidden"; ; } Note: also changed code in div to Code: <div id="show" onmouseover="details_visible(1)" onmouseout="details_hidden(1)"> Hover over me</div> <div id="menuDet1" style="visibility: hidden;" >Plesam Plesam Plesam Plesam Plesam</div> I do not see off the bat what I was doing wrong- anyone else see what I did incorrectly? The script i'm trying to write consist of asking the user for his name in a form format which is then sent in a cookie. When the user reconnect to the same page, it displays : Hi, "HISNAME", welcome to my site! I don't know what I'm doing wrong, seriously. <!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> <title>Untitled Document</title> <script type="text/javascript"> if ( ! get_cookie ( "username" ) ) { var username = document.write( <form name="name"> <table> <tr> <td valign="top"> Name : </td> <td> <input type="text" name="texte" value=""> </td> <td> <INPUT TYPE="button" NAME="bouton" Value="Click" onClick="testResults(this.form)"> </td> </tr> </table> </form>); if ( username ) { var current_date = new Date; var cookie_year = current_date.getFullYear ( ) + 1; var cookie_month = current_date.getMonth ( ); var cookie_day = current_date.getDate ( ); set_cookie ( "username", username, cookie_year, cookie_month, cookie_day ); } } else { var username = get_cookie ( "username" ); document.write ( "Hi " + username + ", welcome to my website!" ); document.write ( "<br><a href=\"javascript:delete_cookie('username'); document.location.reload ( );\"> Forget about me!</a>" ); } </script> </head> <body> </body> </html> |