JavaScript - Javascript If... Statement.
I'm very new to javascript in general, but here's what I'm looking to do:
I have a navigation link ("galleries") that 'onclick' displays a sub-menu of other links (the specific galleries). This is on the home page of my site. When a user first views the home page, the sub-menu does not display, until they click "galleries". On other pages of my site, the "galleries" link is one of the options in my navigation. That link takes users back to the home page which is what I want it to do. But what I'm looking for to happen is if the user is directed back to the home page from within another page on my site, I'd like the link on the homepage to automatically display the sublinks under the "gallery" link. This is so that users don't have to click "galleries" twice in order to see the submenu. For example: If a user is on the 'about me' page, in the navigation there is a 'galleries' link. If they click it, it takes them to the home page. They would then have to click on 'galleries' again on the home page in order to display the sub-menu of individual galleries. I want them to show up immediately, but only when directed back to the home page from within my site. I think I do that with an 'if' statement, but I'm not sure of the syntax. I know that's a bit wordy and a little round about but I hope you can understand what I mean. Feel free to ask any questions if I can clarify something for you. Similar TutorialsI am looking to validate a drop down list box (Motor_Frame), so when "Num" is selected from it, a value must be entered into the Motor_Frame_num text box. I am trying to use the following code but its not working: <script type="text/javascript"> function validate_form (){ Motor_Frame = document.getElementById('Motor_Frame'); if(Motor_Frame.value.toString() != "Num") else if (Motor_Frame_num.value!="" ) { alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500") document.forms[0].Motor_Frame_num.focus(); return false; } return true; } </script> Hey helpful folks! I am completely new to Javascript & I have a basic flash gallery that I embedded into my site (HTML & CSS). When viewed in Firefox everything is fine (840px by 427px), but when viewed in IE the gallery changes size & needs to be (887px by 440px) to be viewed correctly. So, I put an Javascript If Then Else statement. Now it targets and changes in the IE browser (width='887' height='440') , but it also changes the Firefox one to 887x440 when it should stay the original 840x427. Please Help!! Thanks! Code: <div id="gallery"> <SCRIPT language="JavaScript"> <!-- var browserName=navigator.appName; if (browserName=="Microsoft Internet <http://www.pageresource.com/jscript/jbrowse.htm#> Explorer") { document.write("<embed src='PhotoSlider.swf' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='840' height='427' ></embed>"); } else { document.write("<embed src='PhotoSlider.swf' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='887' height='440' ></embed>"); } //--> </SCRIPT> </div> Hey helpful folks! I am completely new to Javascript & I have a basic flash gallery that I embedded into my site (HTML & CSS). When viewed in Firefox everything is fine (840px by 427px), but when viewed in IE the gallery changes size & needs to be (887px by 440px) to be viewed correctly. So, I put an Javascript If Then Else statement. Now it targets and changes in the IE browser (width='887' height='440') , but it also changes the Firefox one to 887x440 when it should stay the original 840x427. Please Help!! Thanks! <div id="gallery"> <SCRIPT language="JavaScript"> <!-- var browserName=navigator.appName; if (browserName=="Microsoft Internet <http://www.pageresource.com/jscript/jbrowse.htm#> Explorer") { document.write("<embed src='PhotoSlider.swf' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='840' height='427' ></embed>"); } else { document.write("<embed src='PhotoSlider.swf' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='887' height='440' ></embed>"); } //--> </SCRIPT> </div> Hi, here is my code: Code: <script> $(window).load(function(){ var totalWidth = 0; $("#gallery img").each(function(){ totalWidth += $(this).outerWidth()+20; }); //alert(totalWidth); a = totalWidth+830; $('#content').css({'width' : a+'px'}); }); $('.back a').attr('href','#X'); $('.back a').click(function(){ var halfA = a/'3'; //alert(halfA); $('html, body').animate({scrollLeft: 0}, halfA); }); </script> on the line that reads: a = totalWidth+830; Is it possible to say something like: IF #content 'contains images' THEN a = totalWidth+430; ELSE a = totalWidth+830; OR the other way around: IF #content 'contains <p>' THEN a = totalWidth+830; ELSE a = totalWidth+430; I wish to do the AND statement in an IF I would try it but have already frozen myself out trying other things with the server that i do not want to cause anymore problems with them. i.e. Code: if (string1 = "word" AND string2 = "word2") { // do something } Hi, I am making a link to the current url and adding ?XXXX to the end of the URL. I have done this like so: Code: <UrlAction Url="javascript:q=(document.location.href);void(open((q)+'?XXXX,'_self','resizable,location,menubar,toolbar,scrollbars,status'))"/> This works perfectly. However I need to add an If statement in this code which does the following: If (there is a ? already in the current url) add &XXXX instead. Can anyone help me add this in? Thanks Hi, I have the below code (in a smarty .tpl file), but would like to automate it with data from MySQL DB and PHP So for example if I was to add another gift_id radio box etc.. in MySQL it would add on to the end extending the if statement to make sure a radio box has been picked. if ((form.gift_id[0].checked == false) && (form.gift_id[1].checked == false) && (form.gift_id[2].checked == false)) { ErrorCount++; But the problem is I am not sure how to do this, any ideas? Thanks. ps. sorry if I posted this in the wrong forum, wasn't sure where to put it Hello, I came across a very odd browser behavior when trying to modify a css class using javascript and at the same time having a base html statement in my html file. Without the base html statement, all browsers work fine and I can change the css class definition using javascript easily. With a base html statement, only FireFox still works while Internet Explorer and Google Chrome dont work anymore. If there is a cross-domain issue, while one browser does work and the others dont? An example of what I'm talking about, with the base statement: http://freebsdcluster.org/~casaschi/...mple-base.html Without the base statement: http://freebsdcluster.org/~casaschi/...le-nobase.html Any idea how to tweak the code in the case with the base html statement in order for the javascript to work with all browser (modifying the class definition) ??? Keep in mind, I dont want a suggestion of another way to change the color, this is only an example. I want to be able to manipulare css classes with javascript when a base html statement is in my html code. This is essentially the code: Code: <!-- --> <base href='http://www.google.com'> <style id='myStyle' type='text/css'> .myStyle { color: red; } </style> <script type="text/javascript"> var lastColor = 'red'; function toggleColor() { lastColor = lastColor == 'red' ? 'green' : 'red'; myObject = document.getElementById('myStyle'); if (myObject.sheet) { myObject.sheet.cssRules[0].style.color = lastColor; } // Mozilla style if (myObject.styleSheet) { myObject.styleSheet.rules[0].style.color = lastColor; } // IE style } </script> <a href="javascript: toggleColor();"><span class=myStyle>click here to toggle color</span></a> Thanks in advance... Hello everyone, I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement. Basically another part of the function is dependent on what the user selects in the radio button. Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works): Code: <script type="text/javascript"> function getSelectedRadio() { len = document.dates.dep.length // returns the array number of the selected radio button or -1 if no button is selected if (document.dates.dep[0]) { // if the button group is an array (one button is not an array) for (var i=0; i<len; i++) { if (document.dates.dep[i].checked) { return i } } } else { if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero } // if we get to this point, no radio button is selected return -1; } function Calculate() { var i = getSelectedRadio(); if (i == -1) { alert("Please select if Marine entered Delayed Entry Program"); } else { if (document.dates.dep[i]) { return document.dates.dep[i].value; } else { return document.dates.dep.value; } } if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && first return from above ) { document.dates.yearDEP.value = (document.dates.yearDEAF.value); document.dates.monthDEP.value = (document.dates.monthDEAF.value); document.dates.dayDEP.value = (document.dates.dayDEAF.value); document.dates.yearPEBD.value = (document.dates.yearDEAF.value); document.dates.monthPEBD.value = (document.dates.monthDEAF.value); document.dates.dayPEBD.value = (document.dates.dayDEAF.value); } else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && second return from above ) { document.dates.yearPEBD.value = (document.dates.yearAFADBD.value); document.dates.monthPEBD.value = (document.dates.monthAFADBD.value); document.dates.dayPEBD.value = (document.dates.dayAFADBD.value); document.dates.yearDEP.value = "N/A"; document.dates.monthDEP.value = "N/A"; document.dates.dayDEP.value = "N/A"; } } </script> I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated! I want to know how I can update my power_points field if a select option is chosen. For example: if animal guardian is chosen Power_points - (attribute_level * 1) if combined attacks is chosen power_points - (attribute_level * 1) if elemental control is chosen power_points - (attribute_level * 3) etc... Here is my current script: Code: <?php session_start(); ?> <form> <table cellpadding="3" cellspacing="3"> <tr> <th>Attribute</th> <th>Level</th> <th>Power Points:</th> <th>Character Points:</th> </tr> <tr> <td><input type="text" readonly="readonly" value="Scout/Knight Powers" size="19" name="attribute1" /></td> <td><select name="level" onclick="power(), remain()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td align="center"><input type="text" name="power_points" id="power_points" size="1" readonly="readonly"/></td> <td align="center"><input type="text" name="cp_remain" id="cp_remain" size="1" readonly="readonly" /></td> </tr> </table> </form> <form method="POST" action="test_batosai.php"> <table><tr><td> <div id="dynamicInput1"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Sub-Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute[]" size="1"> <option value="1">Animal Guardian</option> <option value="2">Combined Attacks</option> <option value="3">Elemental Control</option> <option value="4">Emotional Control</option> <option value="5">Item Of Power</option> <option value="6">Knight Attack</option> <option value="7">Rejuvenation</option> <option value="8">Sailor Scout Attack</option> </select></td> <td> <select name="attribute_level[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="sub_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv1"> </div> </td> <td> <div id="dynamicInput2"> <table cellpadding="3" cellspacing="3"> <thead> <tr> <th>Neutral Attribute</th> <th>Level</th> <th>Notes <i>(optional)</i></th> </tr> </thead> <tbody> <tr> <td> <select name="attribute2[]" size="1"> <option value="1">Acrobatics</option> <option value="2">Appearance</option> <option value="3">Art Of Distraction</option> <option value="4">Combat Mastery</option> <option value="5">Damn Healthy!</option> <option value="6">Divine Relationship</option> <option value="7">Energy Bonus</option> <option value="8">Extra Attacks</option> <option value="9">Focused Combat</option> <option value="10">Fortified Body</option> <option value="11">Heightened Negaverse Power</option> <option value="12">Heightened Senses</option> <option value="13">Heightened Scout Powers</option> <option value="14">Massive Damage</option> <option value="15">Powerful Mind</option> <option value="16">Special Attack/Defense</option> <option value="17">Speed</option> <option value="18">Strong Soul</option> <option value="19">Supernatural Training</option> <option value="20">Unique Character Attribute</option> </select></td> <td> <select name="attribute_level2[]"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select></td> <td><textarea name="neutral_notes[]" rows="1" cols="30"> </textarea></td> </tr> </tbody> </table> </div> <div id="cloneDiv2"> </div> </td></tr></table> <input type="button" value="Add another Sub-Attribute" onClick="addInput('dynamicInput1','cloneDiv1');"> <input type="button" value="Add another Neutral Attribute" onClick="addInput('dynamicInput2','cloneDiv2');"> <input type="submit" /><input type="reset" /> </form> <script type="text/javascript"> var counter = 1; var limit = 10000000;// Set a limit if you want to function addInput(divFrom,divTo){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = document.getElementById(divFrom).innerHTML; document.getElementById(divTo).appendChild(newdiv); counter++; } } //function for Power Points function power() { var first,res1; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res1=(first)*10; //show the result in the result textbox document.forms[0].power_points.value=res1; } //end function for Power Points //function for Character Points remaining function remain() { var first,res2; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].level.value); res2=10-((first)*4); //show the result in the result textbox document.forms[0].cp_remain.value=res2; } //end function for Character Points </script> Can anyone help? Code: function showResults( results ) { if ( results.length == 0 ) { msg = "no projects found"; } else {msg = results [0]; } document.getElementById("RESULTS").innerHTML = msg; //= results.join( "<br/>" ); cannot get it to show "no projects found" for 0 results and also to show all multiple results if multiple are found. works right now for 1 result but not multiple. the =results.join( "<br/>" ); works if multiple but then it will not say no projects found for 0 results. Hello, I have just started studying JS and don't know a whole lot about it. One exercise I have been given is to use the while statement to ask the user to guess a number between one and ten. this is the code I have attempted so far but it does not work. Simple for any one who is good at this. Impossible to me. Any takershttp://www.codingforums.com/images/smilies/confused.gif <!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>While Test</title> <script type="text/javascript"> <!-- function whileTest(){ var number = 4; var answer = 0; while(number != answer ){ answer=prompt("Guesss a number between 1 and ten",""); } alert("4 is the correct answer") //--> </script> </head> <body onLoad="whileTest();"> </body> </html> Hi! I am a total newbie I'm afraid but I really enjoy learning javascript. Today I'm trying to write an if statement. I want the image1 on a web page to change when it's clicked to image2. Then I want the if statement to see if the source of the image is the changed image2.jpg (or still the original image1.jpg) and if it is the changed one I want another click to change it to image3. Here's my code, but the change to image 3 is not working: /* image change start */ var imgchanger = document.getElementById("changingimage"); imgchanger.onclick = function() { imgchanger.src = "image2.jpg"; }; /* image change 2 start */ if (imgchanger.src == "image2.jpg") { imgchanger.onclick = function() { imgchanger.src = "image3.jpg"; }; } Hello CF im wondering how could i use an if statement to have something pop up when visitors visit my site that allows them two choices. it would say something like this. Would you Like to always see the navigation Bar? then they have two options. Yes and No. then depending on what they choose it will either have the code use the absolute or the fixed positioning attribute for the whole code. Or is this just not possible? If you want me to explain it more just ask. can someone help me with this pls: Code: function anything() { var tegenover = document.getElementById("minlas"); var septo = document.getElementById("combo_2"); //prijs verbinder per meter var serr = document.getElementById("comba_2"); //prijs verbinder per meter var seprr = document.getElementById("combb_2"); //prijs verbinder per meter var septrr = document.getElementById("combc_2"); //prijs verbinder per meter var nocho = document.getElementById("wide"); //breedte band in mm document.getElementById("totprijslas").value = Math.round(100*((parseFloat(septo.value) + parseFloat(serr.value) + parseFloat(seprr.value) + parseFloat(septrr.value)) * parseFloat(nocho.value))/1000)/100; } the value returned of totprijslas can be anything between 0 and 1000,but i want the value of "totprijslas" to be a minimum of "50",so for example if result is 30 then i need it to show 50.can someone help me with an if statement for this.thx Code: <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; if (clicks>2) { var answer = confirm ("What choice would you like?") if (answer) window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no') else (answer) window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no') } } </script> OR Code: <script type="text/javascript"> var clicks=0; function linkClick(){ document.getElementById('clicked').value = ++clicks; if (clicks>2) { var answer = confirm ("What choice would you like?") if (answer){ window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no') } else (answer) window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no') } } </script> OK, so now I'm trying so open a specific page depending on the users answer. I am aware that my code should look like this: Code: if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } However because of my click counter I have been unable to do this. My code itself does kinda work BUT choiceA open both choiceA and choiceB's pages :-( choiceB is fine and opens choiceB's page. I know that it's probably a small syntax error but I just can't see where. Thank-you in advance for your assistance. I am relatively new to JavaScript coding, and I a having some problems with an IF statement. I want the IF statement to tell me when the number is below the minimum. Code: function main (txtpamphletMin,txtpamphletMax,optQuality,selIncrement,txtResults) { var txtpamphletMin,txtpamphletMax,optQuality,selIncrement; var amount = 100; var errorMsg = ""; var results = ""; pamphletMin = pamphletMin.value; //Make sure that the minimum number of pamphlets required is entered if (pamphletMin == "pamphletMin<100") { errorMsg = "Number of pamphlets must be above 100"; } else { pamphletMin = txtpamphletMin.value; //get the number of pamphlets from the text box errorMsg = validatepamphletMin(Minimum Pamphlets); } I know that this is no the entire statement but I am just having problems with this one. Can anyone help? I've got this code below to change an images source url based on the clicks from a dropdown menu. Is it possible to shorten this with some sort of if/or statement instead of all the else if's? something that says "if crenate or serrate or this or that, do this" here's the function: Code: function change3(picName,choice) { var url = (document[picName].src); if (/crenate\D{1}/.test(url)) { url = url.replace(/crenate/, choice) document[picName].src=(url); } else if (/margin_finelyserrate\D{1}/.test(url)) { url = url.replace(/margin_finelyserrate/, choice) document[picName].src=(url); } else if (/margin_lobed\D{1}/.test(url)) { url = url.replace(/margin_lobed/, choice) document[picName].src=(url); } else if (/margin_undulate\D{1}/.test(url)) { url = url.replace(/margin_undulate/, choice) document[picName].src=(url); } else if (/doubleserrate\D{1}/.test(url)) { url = url.replace(/doubleserrate/, choice) document[picName].src=(url); } else if (/leaf_shapes\D{1}/.test(url)) { url = url.replace(/leaf_shapes/,"leaf_shapes/" + choice) document[picName].src=(url); } } The dropdown menu just has options written like this: Code: <li id="margin1"><a href="javascript:passit4('entire')" onMouseover="change1('pic8','image34')" onclick="ShowContent('uniquename3'); change3('pic2','entire');" onMouseout="change1('pic8','image_off');">sample link (entire)</a></li> I'm not very familiar with java script at all, but could someone please explain how to make an if else else statement that follows these guidelines? 1. If the income is $70,000 or above the tax rate starts at 70% (the % sign will not show) 2. If the income is $20,000 or below the tax rate starts at 10% (the % sign will not show) 3. Otherwise the tax rate starts at 25% Thank you SO much! |