JavaScript - Newbie Question
Thinking in Objects
4 objects w/constructors/overloads min 3 properties on each min 3 methods total was just wondering if anyone had any good links or tutorials to get me started on my first javascript assignment Similar TutorialsI'm in a computer coding class, and I'm having a bit of trouble with Javascript. I'm not looking for anyone to do my homework for me, but any hints you could give me as to why this code isn't working would be greatly appreciated. Basically, I need an empty text box where the user enters their guess as to my age. The user enters their guess, and then gets an answer as to whether their guess is right, too high, or too low. The page looks right, but I can't get the function to work. Thank you... <html> <head> <script> function howoldami(age) if(age=23) { alert("You guessed right!"); } else if { (age>23) alert("I'm not that old."); } else { alert("I'm not that young."); } </script> </head> <body> <form> Guess My Age!<input name="age"><br> <input type=button value=Guess! onclick="howoldami(age.value)"> </form> </body> </html> Hi Everyone I'm learning Javascript with varying degree's of success but am very much in the early phase. My question/problem is this: [ICODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <script language="javascript" type="text/javascript"> var degFahren = new Array(212, "string data", -459.67); var degCent = new Array(); var loopCounter; for(loopcounter =0; loopCounter <= 2; loopCounter++){ if(isNaN(degFahren[loopCounter])){ alert("Data " + degFahren[loopCounter] + " at array index " + loopCounter + " is invalid"); continue; } degcent[loopCounter] = 5/9 * (degFahren[loopCounter] -32); } for (loopCounter =2; loopCounter >=0; loopCounter --){ document.write("Value " + loopCounter + " was " + degFahren[loopCounter] + " degrees Fahrenheit"); document.write(" which is " + degCent[loopCounter] + " degrees centigrade<br />"); } </script> </body> </html> /ICODE] The following code does not return any errors in my browser but I'm confused since I'm sure I should be seeing an alert box telling me that there is invalid data entered bu this doesn't happen, also as far as I can make out I should only be receiving an error for the 2nd loop but there seem to be an error message returned for all 3 loops?? This is all possibly correct but I'm learning from a book and I have no-one to ask these mundane questions to. Any help would be greatly received. Sara Hi! Im new to the world of javascript, so bare with me. It might be a stupid question but i got a input form, i know how to get the data in but i need to single out one of the numbers. the input looks like: xxxxxx-xxxx I've tried almost everything that i can think of... A nudge in the right direction would be appreciated. Hello, I'm in the process of getting a better understanding of javascript (instead of just copying and pasting JQuery script onto my site). So I have a(dumb?) question. Can I name a var what ever I want? For example, var car_name("Ford Escape"); Or am I misunderstanding this? THanks. This is what I am aiming for. I want to be able to click on a word that would then write text in a specified area. This is what I came up with but it writes it at the top of the page and only for a moment then disappears.... These are my clickable words Code: <a href="#textarea" onClick="MathHomework('W2')">Week 2</a> <a href="#textarea" onClick="MathHomework('W3')">Week 3</a> the anchor named 'textarea' is where I want the output to be written which is placed on my html page. This is my newbie script Code: <script type="text/javascript"> function MathHomework(week) { if (week == "W2") document.write("<p>Review Chapter Twelve</p>"); else if (week == "W3") document.write("<p>Final Chapter Review Test</p>"); } </script> <a name="textarea"> (text area for javascript output)</a> so is my onclick syntax wrong maybe? and yes I have read rule#5 and I am trying to understand my mistakes. Thank you in advance Hi, I'm just starting out with a few bits of javascript and have hit a problem. The story is this: I have a basic HTML page that I want to display using a different css file depending on the page that the request comes from. I though I'd do this by appending "?cs=a" or "?cs=b" depending on the calling page. My problems are occuring whilst trying to read the data in the URL (querystring?) The code below is meant to be a bit of a debugger sort of affair in that it should display the variables but for the life of me I can't get it to work. The problem is that none of the variables seem to be getting populated with data, therefore nothing is displayed on the page and no css file is loaded. I'm pretty sure its something relatively simple so would appreciate if an expert could cast their eye over what I have below and point out the error of my ways... Code: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <!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> <SCRIPT type="text/javascript"> var thequerystring = new String(Request.QueryString("cs")); if (thequerystring == "a") { cssfile = "css1.css"; } if (thequerystring == "b") { cssfile = "css2.css"; } document.write('<link href="'+cssfile+'" type="text/css" rel="stylesheet">'); </SCRIPT> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p><span class="texttype">hello</span></p> this should change the format of the text depending on the css file that was loaded... in theory!! <p> </p> <p>Here is a list of the variables and their values... <script type="text/javascript"> document.write('<b>Some variables...</b><br>'); document.write('cssfilename: '+cssfile+'<br>'); document.write('Querystring text: '+thequerystring+'<br>'); </script> </p> <a href="javascript: history.go(-1)">Back <-- </a> </body> </html> Many thanks in advance Dan I've had several problems with document.getElementById() and haven't been able to find a reason for this. I've searched these forums and I'm sorry if I've still managed to miss if there's an answer to this, as it seems to me to be a basic question. When I use getElementById() it keeps returning as 'undefined' in both FF och IE. I've had to use getElementsByName() instead, and since I only have one element, I have to write element[0] to make it work which seems rather redundant. This is parts of my JS code: Code: var postName = document.getElementsByName("postName"); //getElementById doesn't work here var postText = document.getElementsByName("postText"); //...nor here. if (postName[0].value == "" && postText[0].value == "") { document.getElementById('feedback').innerHTML = "Some text."; //For some stupid reason getElementById works here! } And the HTML looks somewhat like this: Code: <div class="addName" id="addName"> <input type="text" name="postName" id="postName" class="textBox"></input> </div> <div class="addText" id="addText"> <textarea class="textarea" name="post" id="postText"></textarea> </div> <div class="feedback" id="feedback"></div> I don't see why it wouldn't work, but this isn't the only time it's happened so I must be missing something. Anyone who can enlighten me as to what that is? I have yet to have my ah-ha moment with Javascript but I think its coming soon. there are small things that seem to be baffling me and Im hoping posting to the forums to get help from people of limitless wisdom. What exactly does the "|" mean in this code? or I should say what does it do? Code: document.template.Aprod_string.value = document.template.Aprod_string.value + "|" + string_recode(Aprod_array) ; Hi, I am just beginning out with Javascript and trying to complete a simple login page. Clicking on the Login button should bring up a message 'Access authorised' or 'Access denied' in the 'output' div of the page. Unfortunately only 'Access Denied' displays, even if the password entered is correct. There are no messages displayed in the Error Console. Any advice greatly appreciated! Code: <html> <head> <title>Function Example</title> <script language="javascript" type="text/javascript"> function isAccessAuthorised(enteredPassword) { var result; if(enteredPassword=="password") { result=true; } else { result=false; } return result; } function buttonClicked() { var isAuthorised = isAccessAuthorised("enteredPassword").value; if(isAuthorised==true) { document.getElementById("output").innerHTML=<p>Access authorised</p>; } else { document.getElementById("output").innerHTML=<p>Access denied</p>; } } </script> </head> <body> <form> <input type="text" id="username" /> <br/> <input type="text" id="enteredPassword" /> <br/> <input type="button" onClick="buttonClicked()" value="Login"> </form> <div id="output"> </div> </body> </html> Not much programming experience, but some C++ and Python. I would like to make a web page that 1. flashes and image for 1 second 2. flashes a a pair of images for 1 second 3. displays an image and waits 4. Wait for a certain correct keystroke to be entered. 5. Repeat 1-4 a few hundred times. ^The above would iterate through a loop, say of 300 counts, and sub-iterate through an array of images, say 30, going through all 30 images 10 times for example. Oh, and the array elements would be displayed in random order. Also, I would like to keep track of errors (wrong keystroke) and time it took to enter correct keystroke. I would prefer that this webpage work on the iphone, even older ones. Java is limited on iphone? I tried playing a Java chess game and had no luck on iphone. So, would I be able to do this using Javascript? If not, Java would be best? As I said, I would prefer iphone compatibility, but it is not required. I just want to know which one I should use before I bury my nose in a book about programming Java or Javascript. hi. i know a little php, but new to javascript. much of the syntax is identical. in reading the new rhino book (flanagan 6th), he gives the following example: Code: var square = function(x) { return x*x; }; okay, but then he does this: Code: square(plus1(y)) as an example of "invoking two functions in one expresson". (Prior he gives an example of defining a function with plus1, which is how I normally think of functions.) uh, square is a *variable*, but he is using it as a FUNCTION. Can somebody help me understand how this can be? how it works? I'm having trouble conceptualizng how we use a variable as a function. Hi I'm trying to get a slideshow working but the timings are all over the place. I can make it loop through once, neatly, but thereafter the timings seem to out - I have a loop set up to run five times. I think this is because the function is incrementing the delay values or something along those lines (I know there's a neater way to write this but I'm just starting out). Code: function slideWinder () { var x = 1 for (var x = 1; x < 5; x++) { $('#slideone').delay(0).fadeIn(0).delay(2000).fadeOut(400); $('#slidetwo').delay(2000).fadeIn(400).delay(2000).fadeOut(400); $('#slidethree').delay(4000).fadeIn(400).delay(2000).fadeOut(400); $('#slidefour').delay(6000).fadeIn(400).delay(2000).fadeOut(400); $('#slidefive').delay(8000).fadeIn(400).delay(2000).fadeOut(400); $('#slidesix').delay(10000).fadeIn(400).delay(2000).fadeOut(400); } } hi. i know a little php, but new to javascript. much of the syntax is identical. i'm reading the new rhino book (flanagan 6th). in it, where he introduces METHODS, he says "When we combine FUNCTIONS with OBJECTS we get METHODS". Then he creates an empty ARRAY: Code: var a = []; then he uses the "push() method" to add elements to the array. Code: a.push(1,2,3); uh, methods are for *objects* right? Yet he is using them on an ARRAY. Can somebody help me see the light on how an array can magically becomes an object that is manipulated by a "method"? I mean, the array is still an array, no? It never actually becomes an object, right? Yet we still use a *method* to manipulate it. See my conceptual quandry? I have a website, http://www.marcusuav.com and I have the navigation menu setup so that a java-script drop down menu is shown when it is scrolled over. Sometimes when using firefox from different computers, users are complaining of misaligned navigation, when other times, the navigation can seem ok with firefox. I have no idea what to do here, I know add more scripting for this but what? Also, how do I make sure that other browsers show this navigation correctly?
Hi Everyone I am trying to write some code to create an object in JS that contains the x,y coords for a shape i wish to draw. I create the shape, and then in a draw function I draw the shape to the screen from the object I created erlier. Trouble it is does not seem to work :S have anyone got and ideas? Code: var myShape = new newShape(200,200); function init() { canvas = document.getElementById("canvas"); ctx = canvas.getContext("2d"); return setInterval(draw, 50); } // init function newShape(x,y){ this.un_X=x this.un_Y=y function drawAll() { ctx.beginPath(); ctx.arc(un_X,un_Y,30,0,Math.PI*2,true); ctx.closePath(); ctx.stroke(); } // drawAll } // newShape function draw() { ctx.clearRect(0, 0, WIDTH, HEIGHT); myShape.drawAll(); } // draw init(); Thanks in advance Easier to show than to explain: Code: <head> <SCRIPT LANGUAGE="JavaScript"> function checkAll(field) { for (i = 0; i < field.length; i++) field[i].checked = true ; } function uncheckAll(field) { for (i = 0; i < field.length; i++) field[i].checked = false ; } </script> </head> <body> <form name="myform"> <table> <tr id="CA" name="list" value="1" style="background-color:yellow;"> <td> <input type="checkbox" name="list" value="1" onclick="document.getElementById('CA').style.backgroundColor=this.checked?'white':'cyan';">Choice CA</td> </tr> <tr id="CB" name="list" value="1" style="background-color:yellow;"> <td> <input type="checkbox" name="list" value="1" onclick="document.getElementById('CB').style.backgroundColor=this.checked?'white':'cyan';">Choice CB</td> </tr> <tr id="CC" name="list" value="1" style="background-color:yellow;"> <td> <input type="checkbox" name="list" value="1" onclick="document.getElementById('CC').style.backgroundColor=this.checked?'white':'cyan';">Choice CB</td> </tr> </table> <input type="button" name="CheckAll" value="Check All" onClick="checkAll(document.myform.list)"> <input type="button" name="UnCheckAll" value="Uncheck All" onClick="uncheckAll(document.myform.list)"> </form> <p>All never touched should be yellow. All checked should have white background. All unchecked should have cyan background.</p> <p>How do I get 'Check All' and 'Uncheck All' to obey the tr background color, too?</p> hello, is there a date format for an existing date? all I see is a var d = new Date(); but i need to format an existing date? thanks! Function findsometing() ( var myString = 'results' + '<BR>' for (var initiator, test condition,incrementor.) { if (.....) { myString ='some quote'; } else if (.....) { myString += something + '<br>' } else if (....... ) { myString += something + '<br>' } else { myString += ''; } } displayMessage(myString) ) I am a complete newbie to javascript and programming, so excuse any faux pas, but I want to check the myString variable after all the loops in the for loop statement have been completed. For example if nothing was added to the var myString after the completeion of the for loop, i want to display a message that no results found. i tried various attempts of an if statement along lines of } } if (myString = 'results') ....i also tried if myString= ('results' + '<BR>') { displayMessage('no results found') } else { displayMessage(myString) } ) Any pointers as to where i am going wrong? Hi everyone, I was assigned to find out how to do this for my work. We were hoping for a javascript (but it could be PHP or other comparable format). Instead of the search function searching the website, we would like it to search a small database full of information. I have been reading for days now and have just gotten more confused. One thing I read (hopefully it was true) is that you can save a database as an HTML, then convert HTML to .JS which javascript will run. Any help would be greatly appreciated! Jeremy from Farmco |