JavaScript - Is If (var==4) {do Something} Valid?
I'm doing an exercise from a Javascript book and can't seem to figure why this would be valid.
Code: if {var == 4} { Do Something } I think it would be invalid because var is a reserved word which means it doesn't have a value. I could be wrong. Similar TutorialsNote that this is not a script in and of itself, just a section of it, and this seems to be the area that is giving me problems, so I thought I'd post here to see whether it was valid code. Code: var coords if(window.location.href == "http://en59.tribalwars.net/game.php?village=114100&screen=place") { coords = prompt("Please enter the coordinates of the village you want to send a fake to.","|"); } coords=coords.split('|'); As I said, don't expect to make sense of it, as it is just a snippet taken from a script, but this is the part of it which is messing up the script, so I wanted to post here to see whether everything in the above code, the syntax of it and the if statement - is valid. Would that if statement, if used in a proper script, be working, or is there something wrong with it which could be giving me errors? Thanks for your help in advance. Code: <input name="1" value="1" id="1" type="radio" onClick="change(this)"> <input name="1" value="2" id="2" type="radio" onClick="change(this)"> <input name="1" value="3" id="3" type="radio" onClick="change(this)"> <br> <input name="2" value="4" id="4" type="radio" onClick="change(this)"> <input name="2" value="5" id="5" type="radio" onClick="change(this)"> <input name="2" value="6" id="6" type="radio" onClick="change(this)"> <br> <input name="3" value="7" id="7" type="radio" onClick="change(this)"> <input name="3" value="8" id="8" type="radio" onClick="change(this)"> <input name="3" value="9" id="9" type="radio" onClick="change(this)"> <div id="tab3continue"> Hello </div> <script> var tab3_button = document.getElementById('tab3continue'); tab3_button.style.visibility = 'hidden'; function change(switchElement) { if ((switchElement.value == '1' || '2' || '3' )&&(switchElement.value == '4' || '5' || '6')&&(switchElement.value == '7' || '8' || '9')) { tab3_button.style.visibility = 'visible'; } else { tab3_button.style.visibility = 'hidden'; } } </script> So I have a div(tab3continue) which is hidden and want is visible if either 1, 2, 3 are selected AND either 4, 5, 6 are selected AND either 7, 8, 9 is selected. so need one from each group to be selected to make visible. Could someone tell me if this code could work or where I am going wrong? Thanks ------------- Jay Is the window.status still useful nowadays or is it outdated? If it is still used, why doesn't this work please? Code: <a href="javascript_status_bar_messages.cfm" onMouseover="JavaScript:window.status='Status Bar Message goes here'; return true" onMouseout="JavaScript:window.status=''; return true">Hover over me!</a> Complete newbie to JS, If button is clicked, want form to be validated. If the validation passes then bring up print dialogue I have got the validation (using dreamweaver) working, however if the form is correct , how do I get the print dialogue (window.print()) to appear? here's the code Code: <script language="JavaScript" type="text/JavaScript"> <!-- function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_validateForm() { //v4.0 var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]); if (val) { nm=val.name; if ((val=val.value)!="" ) { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error(s) occurred:\n'+errors); document.MM_returnValue = (errors == ''); } //--> </script> Code: <td><input name="Name" type="text" id="Name" size="50" maxlength="50"></td> <input name="Button" type="button" onClick="MM_validateForm('Name','','R');return document.MM_returnValue" value="Button" /> I have the google analytics code but it is presenting itself as not valid. Code: <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try{ var pageTracker = _gat._getTracker("UA-xxxxxx-x"); pageTracker._trackPageview(); } catch(err) {} </script> Can anyone help? ALso is this script suitable to link to externally? I have an online shopping cart and some of the products are sold in boxes of 6. So I am trying to write some code that will alert the customer if they have entered a quantity that isn't some multiple of six. I've tried using the modulus operator as well as dividing by 6 and then checking to see if the result is a whole number but inevitably the alert box pops up no matter what I enter. This is what I'm trying to use now: Code: <head> <script type="text/javascript"> function calculate(qtyordered) { if (qtyordered%6!=0) { alert("Blades are sold in boxes of 6." + '\n' + "Please correct your order quantity."); return false; } else { return true; } } </script> </head> <body> <form action="http://ww4.aitsafe.com/cf/add.cfm" onsubmit="return calculate(qtyordered)" method="post"> <input name="qtyordered" type="text" value="0" size="4" maxlength="4"> </body> What am I doing wrong? Is there a better way to check if the entered quantity is a multiple of 6? this page will display new images when you mouseover the red and orange arrows on the menu on the right side in chrome, but not ff, ie, opera, and safari. Any ideas as to why these other browsers aren't running the code like chrome? guessing this is javascript related - anyone want to offer any clues?
|