JavaScript - Problems Checking For A Valid Page
Similar TutorialsComplete 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" /> 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?
Hey, Is there a method to check if a user refreshed a page? I'm designing an online psychological experiment and I want to see if the user refreshed a page (to see if he was trying to see an image that he was supposed to see only once). Thanks, Evening all, I am new here and seeking some helping modifying this existing code that I found so that it will allow me to run 3 scrollers within the same page. I have tried to rename the variables and other elements within the script but still no luck.... I appreciate the responses: Code: <script type="text/javascript"> var goleftimage='images/left_arrow_1.jpg' var gorightimage='images/right_arrow_1.jpg' var menuwidth=380 var menuheight=60 var scrolldir="normal" var scrollspeed=6 var menucontents='<nobr><center>SCROLLING CONTENT HERE</center></nobr>' var iedom=document.all||document.getElementById var rightdircode='onMouseover="moveleft()" onMouseout="clearTimeout(lefttime)"' var leftdircode='onMouseover="moveright()" onMouseout="clearTimeout(righttime)"' if (scrolldir=="reverse"){ var tempswap=leftdircode leftdircode=rightdircode rightdircode=tempswap } if (iedom) document.write('<span id="temp" style="visibility:hidden;position:absolute;top:-100;left:-5000">'+menucontents+'</span>') var actualwidth='' var cross_scroll, ns_scroll var loadedyes=0 function fillup(){ if (iedom){ cross_scroll=document.getElementById? document.getElementById("test2") : document.all.test2 cross_scroll.innerHTML=menucontents actualwidth=document.all? cross_scroll.offsetWidth : document.getElementById("temp").offsetWidth } else if (document.layers){ ns_scroll=document.ns_scrollmenu.document.ns_scrollmenu2 ns_scroll.document.write(menucontents) ns_scroll.document.close() actualwidth=ns_scroll.document.width } loadedyes=1 } window.onload=fillup function moveleft(){ if (loadedyes){ if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth)){ cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px" } else if (document.layers&&ns_scroll.left>(menuwidth-actualwidth)) ns_scroll.left-=scrollspeed } lefttime=setTimeout("moveleft()",50) } function moveright(){ if (loadedyes){ if (iedom&&parseInt(cross_scroll.style.left)<0) cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px" else if (document.layers&&ns_scroll.left<0) ns_scroll.left+=scrollspeed } righttime=setTimeout("moveright()",50) } if (iedom||document.layers){ with (document){ write('<table border="0" cellspacing="0" cellpadding="1">') write('<td valign="middle"><a href="#" '+leftdircode+'><img src="'+goleftimage+'"border=0></a> </td>') write('<td width="'+menuwidth+'px" valign="top">') if (iedom){ write('<div style="position:relative;width:'+menuwidth+'px;height:'+menuheight+'px;overflow:hidden;">') write('<div id="test2" style="position:absolute;left:0;top:0">') write('</div></div>') } else if (document.layers){ write('<ilayer width='+menuwidth+' height='+menuheight+' name="ns_scrollmenu">') write('<layer name="ns_scrollmenu2" left=0 top=0></layer></ilayer>') } write('</td>') write('<td valign="middle"> <a href="#" '+rightdircode+'>') write('<img src="'+gorightimage+'"border=0></a>') write('</td></table>') } } </script> I will include this within a php page and the menu contents will be dynamically pulled from a database, I can handle populating the menucontents however, I have failed to be able to have more than one scroller on the page at once. Thanks 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. Note 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> 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? 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 keep getting myself into trouble because I tend to use my Java reference when coding Javascript, and of course Javascript doesn't support all Java functions. I've just discovered that apparently Javascript doesn't support the trim() function. But curiously, if I code it as: mystring.trim() then Firefox will handle it without any errors. (I haven't actually checked to see if whitespace is removed from around the string.) But when I code it like this: trim(mystring) then firefox gives an error. I've also found a couple of situations where I've made syntax errors, and Firefox gives no error message. This makes debugging tricky, since the only resource I have available is Firefox's Error Console. Safari has no error reporting whatsoever, AFAIK. MSIE seems to find some errors that Firefox misses, but I don't have MSIE on my main computer, so it's a nuisance to use. As a hobbyist, I have no budget to buy Javascript development tools. Is there something free available that does a better job of finding errors? hello, This seems very basic, but I am running into a complication. What I would like to do is check if a certain div is displayed or hidden (really doesnt matter which) then set a map zoom level based upon that. here is my current function. function closeDirections(){ gdir.clear(); if(document.getElementById("river").style.display="none"){ map.setZoom(16); hideID('river'); showID('main'); } if(document.getElementById("main").style.display="none"){ map.setZoom(12); hideID('main'); showID('river'); } } Also here is a link to my map, just in case. map I have tried several variations, but from what I can tell with every instance, it only examines the first and never considers the second "if". I thought to rememdy this issue by using if/else...same issue. The only thing I could think is a nested if statements, but I am not sure what to use as a parent if. I have searched for a while, but cannot seem to find something like an .is_hidden(). Something that I could see if its true/false. Any help would be very much appreciated. I need to check to see if a cookie has been previously created: Code: var startnum = readCookie('a1'); if (document.cookie.indexOf('a1') == -1){ var startnum = "0"; } Should line 2 be: Code: if (document.cookie.indexOf('a1') == null){ ? Does == -1 do the same thing? I made a cool calendar application I want someone to look over it and see if I messed up anywhere cause I have a good feeling I did. It was designed to be an active desktop ^.^ that way I get my desktop image and a calendar ^.^ index.html Code: <html> <head> <title>Desktop</title> <link rel="stylesheet" type="text/css" href="css/mainTemplate.css" /> </head> <body> <img id="bgImage" src="images/nerbian-entrance.jpg" alt="Active Desktop made by Shelby Brittain" /> <table id="mainTable"> <caption id="caption"> </caption> <tr> <th> Sunday </th> <th> Monday </th> <th> Tuesday </th> <th> Wednesday </th> <th> Thursday </th> <th> Friday </th> <th> Saturday </th> </tr> <tr> <td id="d1"> </td> <td id="d2"> </td> <td id="d3"> </td> <td id="d4"> </td> <td id="d5"> </td> <td id="d6"> </td> <td id="d7"> </td> </tr> <tr> <td id="d8"> </td> <td id="d9"> </td> <td id="d10"> </td> <td id="d11"> </td> <td id="d12"> </td> <td id="d13"> </td> <td id="d14"> </td> </tr> <tr> <td id="d15"> </td> <td id="d16"> </td> <td id="d17"> </td> <td id="d18"> </td> <td id="d19"> </td> <td id="d20"> </td> <td id="d21"> </td> </tr> <tr> <td id="d22"> </td> <td id="d23"> </td> <td id="d24"> </td> <td id="d25"> </td> <td id="d26"> </td> <td id="d27"> </td> <td id="d28"> </td> </tr> <tr> <td id="d29"> </td> <td id="d30"> </td> <td id="d31"> </td> <td id="d32"> </td> <td id="d33"> </td> <td id="d34"> </td> <td id="d35"> </td> </tr> <tr> <td id="d36"> </td> <td id="d37"> </td> <td id="d38"> </td> <td id="d39"> </td> <td id="d40"> </td> <td id="d41"> </td> <td id="d42"> </td> </tr> </table> </div> <script type="text/javascript" src="js/mainLib.js"> //<![CDATA[ //]]> </script> </body> </html> css/mainTemplate.css Code: body { background-color: black; color: #C0C0FF; } caption { font-size: 24pt; } th, td { width: 100px; border: 1px solid #C0C0FF; } th { height: 25px; } td { vertical-align: bottom; text-align: right; } #bgImage { position: absolute; top: 0px; left: 0px; width: 102%; height: 103%; } #mainTable { position: absolute; top: 2%; left: 61%; border: 1px solid #C0C0FF; padding: 5px; vertical-align: top; width: 600px; height: 375px; background-color: black; opacity:0.5; filter:alpha(opacity=50); } js/mainLib.js Code: var d = new Date(); var day = d.getDate(); var wk = d.getDay(); var set = new Array(35); var j = 0; var mnth = d.getMonth(); function getMonthName() { var mth = ""; var month = new Array(12); month[0]="January"; month[1]="Febuary"; month[2]="March"; month[3]="April"; month[4]="May"; month[5]="June"; month[6]="July"; month[7]="August"; month[8]="September"; month[9]="October"; month[10]="November"; month[11]="December"; mth = month[d.getMonth()]; return mth; } function getFirstDay() { var firstDay = wk; for(var i=day-1; i>=0; i--) { firstDay--; if(firstDay == -1) { firstDay=6; } } return firstDay; } if(mnth==1 && getFirstDay()!=6) { monthLength = 28; } else if(mnth==1 && getFirstDay()==6) { monthLength = 28+6; } else if(mnth==0 || mnth==3 || mnth==5 || mnth==8 || mnth==10) { monthLength = 30+getFirstDay(); } else { monthLength = 31+getFirstDay(); } document.getElementById("caption").innerHTML = getMonthName(); if(getFirstDay()==6) { for(var i=1; i<=monthLength-6; i++) { j++; set[i] = "d"+ (i); document.getElementById(set[i]).innerHTML = j; } } else { for(var i=getFirstDay()+1; i<=monthLength; i++) { j++; set[i] = "d"+ (i + 1); document.getElementById(set[i]).innerHTML = j; } } Thanks Shelby Hai Friends..I made a registration form.I have used php,html as well as javascript in it.In the onclick event of submit button I've provided a call to a function written in javascript.Things are workin fine i.e validations are working properly and insertion of values to the database is also ok. And the problem dat I have is, I want to check whether username and emailid provided is unique with respect to my site...that is no two users can't have same emailid or username.I'm able to retreive the entire emailid and username out of the database.But I don't know where should I place the code for checking the equality.I think that the code should be placed in the javascript validation function..but the code for retreiving values from db is in php. I'm totally confused.I'm attaching here the registration page that I made...anyone out there please go through it and help me with a solution. Hey guys. I need a little help with checking elements in my array. I created an array of size 50 and initialized all elements to 1. What i want to do is if the user types in the number "1" into the prompt box, i want it to check the first 15 elements and increase only one of the elements by 1, and if the element has already been increased by 1 then i want it to check the next element and see if its been increased, if the element has been increased then it needs to move on to the next one and check that. If the element is 1, then it can be increased by "1", if the element is 2 then i need it to check the next element. If all elements within the first 15 have increased by 1, it should just alert the user that all slots have been assigned. Is there a way to do this? Thanks for your time
Hi. I'm having some trouble with validating all my fields in my form. I'm posting my code here. Code: function validateForm() {var chk = document.forms["EntryPage"]["studentname"].value; if (chk==null || chksdtnm=="") { alert("Student name must be filled out"); return false; } if (chk==null || chksdtnm=="") { alert("Student ID must be filled out"); return false; } } I need to alert the user with a messagebox for six fields if ever they're not filled in. How do I achieve that? Hello all I'm trying to get this function to check for 10 digits only but alert shows regardless of less / more or exactly 10 digits --- what am I missing? any help on sorting this greatly appreciated. Code: function validate(form) { if (document.forms[0].callme.checked) { var phone = document.forms[0].mobile.value; // assign value to var phone = phone.replace( /[^\d]/g, "" ); // zap all non-digit characters if ( document.forms[0].mobile.length != 10 ) // chk for 10 digits{ alert("Enter 10 numbers"); return false; } else { alert("Yahoo"); } } low tech |