JavaScript - Relatively Basic Javascript? Newb..
Hey guys. I am pretty new to JS and programming. The company that I am working for put me on this project and I have a question. Here is a snippit for a form that we have. We use a dreamweaver extension "HDW form to database" to process the forms and put them in a mysql database.
the wpform() is a validation function that has been written. what I want, is the fallowing, if you look at the code below in the option object, there are different categories... rock, funk, hip hop, synth etc. i want it so when one of them is selected, it goes to a different respected page. for example, if the value Rock is selected, and you press submit, it would take you to www.google.com . On the other hand if the value Funk is submitted, it will take you to www.yahoo.com . Code: <form action="/HDWFormToDatabase/FormToDatabase.php" method="post" name="theForm" onSubmit="return wpform()" accept-charset="iso-8859-1"> <select size="1" name="genre"> <option selected="selected" value="genre">Choose a Genre!</option> <option value="Rock">Rock</option> <option value="Funk">Funk</option> <option value="Hip Hop">Hip Hop</option> <option value="80s">Synthesizer</option> <option value="Techno">Techno</option> </select> <input type="hidden" name="hdwok" id="hdwok" value=" URL GOES HERE" /> </form> Normally, when this option object is not present, the user presses submit and after the form gets processed to the database, the browser forwards to the "URL GOES HERE" value. However, I am not sure exactly what to do now since the URL which it is forwarded to will change. can someone give me a hint on how to make this happen? just some general advice for a newb? I would write it but I am pretty lost. I was thinking of including a if statement in the wpform() function that goes something like: Code: if (document.theForm.genre.selectedIndex == 0) { alert("Please select your genre."); document.theForm.genre.focus(); return (false); } if (document.theForm.genre.selectedIndex==1) { window.location = "http://www.google.com/"; return(true); } if (document.theForm.genre.selectedIndex==2) { window.location = "http://www.yahoo.com/"; return(true); } etc etc Would this work? Is there a more efficient way of doing something like this that I should know of due to my lack of JS skills? Thanks in advance so much for help. Similar TutorialsI'm trying to insert a title and a body into a Google maps window through PHP. The body could contain double or single quotes. How do I output these so that they don't break the script? Relevant code: Code: $('#map_canvas').gmap('openInfoWindow', { 'content': '<strong><?php echo $m->marker->Title; ?></strong><br /><?php echo $m->marker->Body; ?><?php echo '<a href="./marker_info.php?id='.$m->marker->Nid.'"></a>' ?><br /><a id="m_location" href="#" data-role="button" data-icon="search" onclick="$.mobile.silentScroll(500);"></a>'}, this); I have a function which I placed in the header of my html file: Code: <script language="javascript" type="text/javascript"> function UngreyInstallButtons() { document.form1.viewbutton1.disabled = false; document.form2.viewbutton2.disabled = false; document.form3.viewbutton3.disabled = false; } </script> I want to call this from within the body of my html page: Code: <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> <!-- var name = GetCookie('COOKIENAME'); if (name != null) { alert("I GET HERE" + name + "!"); UngreyInstallButtons(); alert("I DONT MAKE IT HERE"); } //--> </SCRIPT> The GetCookie func works and returns name correctly. The first alert() box is displayed, - the second one isn't! It does not come back from the call to UngreyInstallButtons() Why? I also tried this: Code: if (name != null) { alert("I GET HERE" + name + "!"); document.form1.viewbutton1.disabled = false; document.form2.viewbutton2.disabled = false; document.form3.viewbutton3.disabled = false; alert("I DONT MAKE IT HERE"); } Again, it does not execute the = false lines at all. Perhaps I'm missing something obvious? (I am new to js) Hi, I'm trying to create a video box on a website that plays a youtube video and then automatically plays random recommended videos with no break in playback. An endless playlist created by youtube based on the initial video. Youtube uses Javascript API. I have never used Javascript before but i'm just trying to get the video to play as above. There are few tutorials online and I couldn't find anyone trying to do a similar thing. If you have any idea how i'd go about it or if not where i could find some information to help. Thanks. Hey guys! New user here and I just need a little help with some basic Javascript I must write a function that 'shows' the corresponding part of the form depending on what the user has selected. So... for example, if I select 'Pizza', the div #section2 with the question about Pizza type should be shown. All the sections that are not selected should be hidden. For example, if 'Mexican' is selected, the sections for 'Pizza' and 'Thai' should be hidden. I want this function to also display a message in the JavaScript console indicating which food type is selected. Apparently I'm supposed to use 'console.log("")'??? I'm using an external javascript file called prac8.js If anyone has any idea how to do this I'd be very greatful Code: <!DOCTYPE html> <html> <head> <title>Prac 8 example</title> <script type="text/Javascript" src="prac8.js"></script> <style> #section2, #section3, #section4 { display:none; border:1px solid gray; padding:8px; margin-top:12px; width:400px; } #dessertmsg { color:green; } </style> </head> <body> <h1>Prac 8 example</h1> <h2>A Food Survey</h2> <form id="survey" action="#" method="post"> <div id="section1"> <label for="food">What is your favourite type of food?</label> <select id="food" onchange="selection()"> <option value="pizza">Pizza</option> <option value="mex">Mexican</option> <option value="thai">Thai</option> </select> </div> <div id="section2"> What is your favourite type of pizza?<br> <label for="hawaiian">Hawaiian</label><input type="radio" id="hawaiian"> <label for="supreme">Supreme</label><input type="radio" id="supreme"> <label for="vegetarian">Vegetarian</label><input type="radio" id="vegetarian"> </div> <div id="section3"> What is your favourite Mexican food?<br> <label for="burrito">Burritos</label><input type="radio" id="burrito"> <label for="chilli">Chilli con carne</label><input type="radio" id="chilli"> <label for="taco">Tacos</label><input type="radio" id="taco"> </div> <div id="section4"> What is your favourite Thai food?<br> <label for="stir">Stir-fry</label><input type="radio" id="stir"> <label for="noodle">Noodles</label><input type="radio" id="noodle"> <label for="curry">Curry</label><input type="radio" id="curry"> </div> <label for="dessert">What is your favourite dessert?*</label> <input type="text" id="dessert" onchange="checkForm()"><br> <div id="dessertmsg"> </div> <input type="submit" value="Submit" id="subbutton" disabled> </form> </body> </html> Hi, I've just started learning JavaScript - actually started about half an hour ago. I have an extensive knowledge of Lua (if you've heard of it), so the concepts of programming aren't new to me. This code isn't doing anything. The HTML code makes the hyper-linked pictured show, but it doesn't do anything when the mouse rolls over it. The JavaScript code is saved in scripts/titleImage_rollover.js, and I'll show the HTML code as well. JavaScript: Code: <!-- // image rollover for header function image_rollover(){ //show a set of random images switch(math.floor(math.random()*5+1)) { case 1: document.getElementById("logo").src = "images/t2m_logo.png"; break; case 2: document.getElementById("logo").src = "images/pighead10_halloween.png"; break; case 3: document.getElementById("logo").src = "images/pighead10_easter.png"; break; case 4: document.getElementById("logo").src = "images/pighead10_classic.png"; break; case 5: document.getElementById("logo").src = "images/pighead10_combat.png"; break; } } //reset image to normal function image_reset(){ document.getElementById("logo").src = "images/pighead10.png"; } //--> HTML: < Code: !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>pighead10.com | Home</title> <style type="text/css"> .link1 A:link{text-decoration:none;} .link1 A:visited{text-decoration:none;} .link1 A:active{text-decoration:none;} .link1 A:hover{text-decoration:underline;color:blue;} </style> <script type="text/javascript" src="scripts/titleImage_rollover.js"></script> </head> <body style="font-family:arial;color:red"> <a href="http://www.roblox.com/User.aspx?ID=2969907"> <img id="logo" src="images/pighead10.png" width="150" height="200" onmouseover="image_rollover()" onmouseleave="image_reset()"/> </a> </body> </html> Also, is there a way to view some kind of output in JavaScript? This must be very simple. I want to execute some javascript only if an element is not displayed What is the syntax? if("#element:hidden) { do something like show it } Help much appreciated. Hello All Its my first post on this forum. I have a database setup with tables that use my form element names as column names. Now i have successfully retrieved column names and values from the server side to the client side. . I have the column name stored in var columnName How can I get the type of form element type and how can i set the value to it Thanks for your help #Javalove. . .unless otherwise stated. . .! hi all, just got to make a basic form for my assignemment but i cant seem to get it to work. please could somebody point me in the right direction? thank you Code: <head> <script type="text/javascript"> function make(myform) { var name = form.nameinput.value ; var age = form.ageinput.value ; var location = form.locationinput.value ; var z = form.team.selectedIndex ; var team = form.team.options[z].value; alert("Thank you " + name + ".Good luck supporting " + team + ); } </script> </head> <body> <form method="get" action="" name="myform"> <h2>Order form</h2> <fieldset> <p>Please enter your details</p> Name: <input type="text" value="" name="nameinput"/><br/> Age: <input type="text" value="" size="2" name="ageinput"/><br/> Location: <input type="text" value="" name="locationinput"/> <p>Choose your favourite football team</p> <select name="team"> <option value="arsenal">Arsenal</option> <option value="aston_villa">Aston Villa</option> <option value="birmingham_city">Birmingham City</option> <option value="blackburn_rovers">Blackburn Rovers</option> <option value="bolton_wanderers">Bolton Wanderers</option> <option value="burnley">Burnley</option> <option value="chelsea">Chelsea</option> <option value="everton">Everton</option> <option value="fulham">Fulham</option> <option value="hull">Hull</option> <option value="liverpool">Liverpool</option> <option value="manchester_city">Manchester City</option> <option value="manchester_united">Manchester United</option> <option value="portsmouth">Portsmouth</option> <option value="stoke_city">Stoke City</option> <option value="sunderland">Sunderland</option> <option value="tottenham_hotspur">Tottenham Hotspur</option> <option value="west_ham">West Ham</option> <option value="wigan_athletic">Wigan Athletic</option> <option value="wolverhampton_wanderers">Wolverhampton Wanderers </option> </select> </fieldset> <input type="button" onclick="make(this.form)" value="Submit Details" name="button"/> </form> Hi, im sorta a newbie but have a few questions i cannot find online anywhere. By the way im trying to create a chrome extension . Its a fairly simple extension, all i want it to do is select a few buttons automatically when the page is loaded. Without me having to click anything. A bot persay. How to automatically select a button/drop down menu How to save a form How to submit a form to a website How it works if you dont get it? Selecting a certain size like S/M/L, color, or anything of that nature AUTOMATICALLY WITHOUT HAVING TO CLICK ANYTHING. Which then adds to my cart. Thats all i want it to really do. Thanks for reading. Hi guys, I am new to learning javascript and have a test tomorrow on for loops. I know the answers to the three following loops but i wondered if anyone could take the time to explain to me how to find the answer from the code as I have no idea on how to find the answers (the answer is what is displayed in the alert). Many thanks, Mike The loops are these three: 1. var a=4;b=6; for(var c=0;c<=a;c++){b=b+c;} alert(b); (answer is 16) 2. var v=1; for(i=1;i<5;i++) for(j=2;j<5;j++) v+=1; alert(v); (answer is 13) 3. var s=0; for(i=0;i<=8;i++) if((i%2)==0)s+=1; alert(s); (answer is 5) Hi so i am using onClick="myPopup(); on a button and when someone clicks on the button, it pops up an advertisement in a new tab. But is there a way to make the users stay on the page where the button is located instead of forcing the browser to automatically switch to the advertisement tab when a button is clicked? Hi, I'm very much a newbie regaridng javascript and have been looking around and experimenting and have created a javascript although it does not function(Javascript code below), can someone help me fix it? Many Thanks. function gotoURL(){ var newURL = document.GotoForm.theURL.value if (newURL == "info1" || "info2" || "info3" || "info4" || "info 5"){ document.location.href=newURL+".html" }else{ alert("You entered an invalid value"); }} I'm stumped. I realise it's something basic... This works - i.e. beeps on mouseover: Code: <html> <head> <script> function EvalSound(soundobj) { var thissound= eval("document."+soundobj); thissound.Play(); } </script> </head> ... <a href="#" onMouseOver="EvalSound('sound1')">Move mouse here</a> ... <embed src="beep.mp3" autostart=false width=1 height=1 name="sound1" enablejavascript="true"> </body> </html> ...but this doesn't work: (I get the error "undefined is null or not an object") Code: <html> <head> <script> function EvalSound(soundobj) { var thissound= eval("document."+soundobj); thissound.Play(); } </script> </head> ... <script language="JavaScript"> <!-- EvalSound('sound1'); change_it1() // activates an external .js //--> </script> ... <embed src="beep.mp3" autostart=false width=1 height=1 name="sound1" enablejavascript="true"> </body> </html> What am I doing wrong? Gotta make the second idea work... Thanks Hello, I am a computing pupil currently taking part in a computer science project at GCSE level. Our project is a research task regarding web form validation, JavaScript and embedding JavaScript into HTML code. I have no prior knowledge of any of these topics, so would be very interested in hearing your opinions on the aboce. I would also like to know your opinion on how effective JavaScript validation is, possibly in your field of work, or just your experience of using it? Thanks in advance, Ellen I have a basic scroller list code, which works when I use it on a HTML page. However if I duplicate it and change the text, only one of the lists functions. I even tried to use a script from a free website online and duplicate that on the page and it still does not work twice. Could somebody help with this? I'm building a website to catalog a history of college football uniforms, and having something like this on several of the pages would be extremely helpful. I've been told that making it in Javascript instead of Flash would be better for my website. I've also been told that it should be relatively easy to write the code for it. In short, I want to be able to take the parts of this image and allow viewers of the site to be able to switch between parts to mix and match. There are quite a few teams with six or more possible uniform combinations, so I'd like to be able to do this for the site. This would probably be a good way to get my start on learning at least some Javascript basics, so would anyone be willing to help me put this together? Hello, Is there a way with JavaScript to easily detect which mobile browser is being used? I would not normally ask this, but because we do not have the time or budget to implement any back end detection scripting it is our only option right now! I don't think we will need to detect which version of the browser, or which revision of the phone is used - just need to target each family to deliver the proper video type. I am a total newb with javascript. i got this slideshow code from the following URL http://javascriptsource.com/miscella...slideshow.html its not working and i dont know why. there were no instructions and i cant figure out why there is only one image listed. i replace the img source with one of of my own but shouldnt there be multiple images? isnt that the point of a slideshow? anyway heres the code: Code: <SCRIPT LANGUAGE="JavaScript"> <!-- Original: Mike Canonigo (mike@canonigo.com) --> <!-- Web Site: http://www.munkeehead.com --> <!-- This script and many more are available free online at --> <!-- The JavaScript Source!! http://javascript.internet.com --> <!-- Begin NewImg = new Array ( "images/1.gif", "images/2.gif", "images/3.gif" ); var ImgNum = 0; var ImgLength = NewImg.length - 1; //Time delay between Slides in milliseconds var delay = 3000; var lock = false; var run; function chgImg(direction) { if (document.images) { ImgNum = ImgNum + direction; if (ImgNum > ImgLength) { ImgNum = 0; } if (ImgNum < 0) { ImgNum = ImgLength; } document.slideshow.src = NewImg[ImgNum]; } } function auto() { if (lock == true) { lock = false; window.clearInterval(run); } else if (lock == false) { lock = true; run = setInterval("chgImg(1)", delay); } } // End --> </script> </HEAD> <!-- STEP TWO: Copy this code into the BODY of your HTML document --> <BODY> <img src="images/1.gif" name="slideshow"> <table> <tr> <td align="right"><a href="javascript:chgImg(-1)">Previous</a></td> <td align="center"><a href="javascript:auto()">Auto/Stop</a></td> <td align="left"><a href="javascript:chgImg(1)">Next</a></td> </tr> </table> </center><p> If someone can help me i would really appreciate it. (NO THIS IS NOT HOMEWORK!) Hello all, I am an admin, and I am building a radial button search option on a webpage. It works fine, except I cant seem to get it to open the results in a new window. I tried using the "window.open" but it didnt work. Any assitance would be appreciated! Also, I am not a great java programmer, and what you see below is the extent of my knowledge Thanks in Advance!! ===================CODE=============== <html> <body> <script language="JavaScript">eng = 'http://www.google.com/search?q=';</script> Search: <input type="text" id="searchterms"> <input type="button" value="Search" onload="" onclick="window.location = eng + escape(document.getElementById('searchterms').value); return false;"> <br /> <input type="radio" onclick="eng = this.value;" checked name="sengines" value="http://www.google.com/search?q=" />Google <input type="radio" onclick="eng = this.value;" name="sengines" value="http://support.citrix.com/search/basic/?searchQuery=" />CTX KB <input type="radio" onclick="eng = this.value;" name="sengines" value="http://support.microsoft.com/search/default.aspx?mode=r&query=" />MS KB </body> </html> I just started with javascript and am working on a little game to wrap my head around it and came across an error I can't seem to fix. Code: if (AsteriodXpos[i] < -50 || AsteriodYpos[i] < -100 || AsteriodYpos[i] > WinHeight + 100) { AsteriodXpos[i] = WinWidth; AsteriodYpos[i] = Math.round(Math.random() * WinHeight); AsteriodXSpeed[i] = Math.random() * 5 + 7; AsteriodYSpeed[i] = Math.random() * 7 - 5; AstriodsAvoided += 1; if (AstriodsAvoided % 15 == 0) { document.write('<div style="position:absolute;top:0px;left:0px">'); document.write('<div style="position:relative">'); document.write('<div id="ast" style="position:absolute;font-size:2px"><img src="asteroid.png" height=50 width=50></div>'); document.write('</div>'); document.write('</div>'); AsteriodYpos[AsteriodYpos.length] = Math.round(Math.random() * WinHeight); AsteriodXpos[AsteriodXpos.length] = Math.round(Math.random() * WinWidth) + WinWidth; AsteriodXSpeed[AsteriodXSpeed.length] = Math.random() * 10 + 5; AsteriodYSpeed[AsteriodYSpeed.length] = Math.random() * 7 - 5; asteriods += 1; } } ast[i].style.pixelLeft = AsteriodXpos[i]; ast[i].style.pixelTop = AsteriodYpos[i]+hscrll; When it seems to not like these last two lines as it says "Error: Unable to get value of the property 'style': object is null or undefined". Essentially it is a game where you are flying through space avoiding astroids. after you avoid so many it increases the number of astroids which is where the problem is happening. In case someone would like to see this in context of what im doing the whole code is Code: <html> <head> </head> <body bgcolor="#000000" onload="fly()" onkeypress="displayunicode(event)" style="overflow: hidden"> > <script language="JavaScript"> <!-- Begin x = 0; var startTime = new Date(); // create object imageObj = new Image(); // set image list images = new Array(); images[0] = "ship1.png" images[1] = "ship2.png" images[2] = "ship3.png" images[3] = "ship4.png"; images[4] = "ship5.png" images[5] = "ship6.png" // start preloading for (i = 0; i <= 5; i++) { imageObj.src = images[i]; } AstriodsAvoided = 1; wasCollision = 0; SmallStars = 20; LargeStars = 50; asteriods = 5; SmallYpos = new Array(); SmallXpos = new Array(); LargeYpos = new Array(); LargeXpos = new Array(); Smallspeed = new Array(); Largespeed = new Array(); AsteriodXpos = new Array(); AsteriodYpos = new Array(); AsteriodXSpeed = new Array(); AsteriodYSpeed = new Array(); document.write('<div style="position:absolute;top:0px;left:0px">'); document.write('<div style="position:relative">'); for (i = 0; i < SmallStars; i++) { document.write('<div id="si" style="position:absolute;top:0;left:0;width:1px;height:1px;background:#fffff0;font-size:1px"></div>'); } document.write('</div>'); document.write('</div>'); document.write('<div style="position:absolute;top:0px;left:0px">'); document.write('<div style="position:relative">'); for (i = 0; i < LargeStars; i++) { document.write('<div id="li" style="position:absolute;top:0;left:0;width:3px;height:2px;background:#ffffff;font-size:2px"></div>'); } document.write('</div>'); document.write('</div>'); document.write('<div style="position:absolute;top:0px;left:0px">'); document.write('<div style="position:relative">'); for (i = 0; i < asteriods; i++) { document.write('<div id="ast" style="position:absolute;top:0;left:0;font-size:2px"><img src="asteroid.png" height=50 width=50></div>'); } document.write('</div>'); document.write('</div>'); imgWidth = 150; imgHeight = 150; document.write('<div style="position:absolute;top:0px;left:0px">'); document.write('<div style="position:relative">'); document.write('<div id="shipLayer" style="position:absolute;top:0;font-size:2px"><img id="ship" src="ship.png" height=150 width=150></div>'); document.write('</div>'); document.write('</div>'); WinHeight = window.document.body.clientHeight; WinWidth = window.document.body.clientWidth; ShipYpos = Math.round(Math.random() * WinHeight); ShipXpos = Math.round(Math.random() * WinWidth) / 2; ShipXSpeed = 0; ShipYSpeed = 0; shipLayer.style.pixelLeft = ShipXpos; shipLayer.style.pixelTop = ShipYpos; for (i = 0; i < SmallStars; i++) { SmallYpos[i] = Math.round(Math.random() * WinHeight); SmallXpos[i] = Math.round(Math.random() * WinWidth); Smallspeed[i] = Math.random() * 5 + 1; } for (i = 0; i < LargeStars; i++) { LargeYpos[i] = Math.round(Math.random() * WinHeight); LargeXpos[i] = Math.round(Math.random() * WinWidth); Largespeed[i] = Math.random() * 10 + 5; } for (i = 0; i < asteriods; i++) { AsteriodYpos[i] = Math.round(Math.random() * WinHeight); AsteriodXpos[i] = Math.round(Math.random() * WinWidth) + WinWidth; AsteriodXSpeed[i] = Math.random() * 10 + 5; AsteriodYSpeed[i] = Math.random() * 7 - 5; } function fly() { document.getElementById("ship").src = images[x]; x += 1; if (x == 6) { x = 0; } var WinHeight = window.document.body.clientHeight; var WinWidth = window.document.body.clientWidth; var hscrll = document.body.scrollTop; var wscrll = document.body.scrollLeft; for (i = 0; i < LargeStars; i++) { LargeXpos[i] -= Largespeed[i]; if (LargeXpos[i] < -10) { LargeXpos[i] = WinWidth; LargeYpos[i] = Math.round(Math.random() * WinHeight); Largespeed[i] = Math.random() * 10 + 5; } li[i].style.pixelLeft = LargeXpos[i]; li[i].style.pixelTop = LargeYpos[i] + hscrll; } for (i = 0; i < SmallStars; i++) { SmallXpos[i] -= Smallspeed[i]; if (SmallXpos[i] < -10) { SmallXpos[i] = WinWidth; SmallYpos[i] = Math.round(Math.random() * WinHeight); Smallspeed[i] = Math.random() * 5 + 1; } si[i].style.pixelLeft = SmallXpos[i]; si[i].style.pixelTop = SmallYpos[i] + hscrll; } for (i = 0; i < asteriods; i++) { AsteriodXpos[i] -= AsteriodXSpeed[i]; AsteriodYpos[i] -= AsteriodYSpeed[i]; //for (j=0; j<30; j++){ // xDistance = AsteriodXpos[i]+25-AsteriodXpos[j]+25; // yDistance = AsteriodYpos[i]-25-AsteriodYpos[j]-25; // distance =xDistance*xDistance+yDistance*yDistance; // if (distance<3000 && i!=j){ // } if (AsteriodXpos[i] < -50 || AsteriodYpos[i] < -100 || AsteriodYpos[i] > WinHeight + 100) { AsteriodXpos[i] = WinWidth; AsteriodYpos[i] = Math.round(Math.random() * WinHeight); AsteriodXSpeed[i] = Math.random() * 5 + 7; AsteriodYSpeed[i] = Math.random() * 7 - 5; AstriodsAvoided += 1; if (AstriodsAvoided % 15 == 0) { document.write('<div style="position:absolute;top:0px;left:0px">'); document.write('<div style="position:relative">'); document.write('<div id="ast" style="position:absolute;font-size:2px"><img src="asteroid.png" height=50 width=50></div>'); document.write('</div>'); document.write('</div>'); AsteriodYpos[AsteriodYpos.length] = Math.round(Math.random() * WinHeight); AsteriodXpos[AsteriodXpos.length] = Math.round(Math.random() * WinWidth) + WinWidth; AsteriodXSpeed[AsteriodXSpeed.length] = Math.random() * 10 + 5; AsteriodYSpeed[AsteriodYSpeed.length] = Math.random() * 7 - 5; asteriods += 1; } } ast[i].style.pixelLeft = AsteriodXpos[i]; ast[i].style.pixelTop = AsteriodYpos[i]+hscrll; } if (ShipXpos - ShipXSpeed < -150) { ShipXpos = WinWidth; } else { if (ShipXpos - ShipXSpeed > WinWidth) { ShipXpos = -150; } } if (ShipYpos - ShipYSpeed < -150) { ShipYpos = WinHeight; } else { if (ShipYpos - ShipYSpeed > WinHeight) { ShipYpos = -150; } } ShipXpos -= ShipXSpeed; ShipYpos -= ShipYSpeed; shipLayer.style.pixelLeft = ShipXpos; shipLayer.style.pixelTop = ShipYpos + hscrll; DidCollide() sleep(10); setTimeout('fly()', 10); } // End --> function displayunicode(e) { var unicode = e.keyCode ? e.keyCode : e.charCode; var keyPressed; keyPressed = (String.fromCharCode(unicode)); moveShip(keyPressed) } function moveShip(keyPressed) { switch (keyPressed) { case "w" || "W": if (ShipYSpeed != 25) { ShipYSpeed += 5; } break; case "a" || "A": if (ShipXSpeed != 25) { ShipXSpeed += 5; } break; ww case "s" || "S": if (ShipYSpeed != -25) { ShipYSpeed += -5; } break; case "d" || "D": if (ShipXSpeed != -25) { ShipXSpeed += -5; } break; } } function DidCollide() { for (i = 0; i < asteriods; i++) { xDistance = AsteriodXpos[i] + 25 - ShipXpos - 75; yDistance = AsteriodYpos[i] + 25 - ShipYpos - 75; distance = xDistance * xDistance + yDistance * yDistance; if (distance < 4225) { wasCollision = 1; document.write('<img src="Explosion.png">'); var endTime = new Date(); var totalTime = new Date(); totalTime.setTime(endTime.getTime() - startTime.getTime()); test = "this is a test"; document.write('You managed to last ' + totalTime.getSeconds() + '.' + totalTime.getMilliseconds() + ' seconds. You avoided a total of ' + AstriodsAvoided + 'astroids'); } } } function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds) { break; } } } </script> <!-- Script Size: 3.79 KB --> </body> </html> Any help someone could provide would be wonderful, and if you can explain to me whats going wrong and why its not working that would be even better! |