JavaScript - Simple Array Question
Ok...this is gonna sound very stupid to you guys, but I don't get one part of this code.
Code: function range(upto) { var result = []; for (var i = 0; i <= upto; i++) result[i] = i; return result; } show(range(4)); What does result[i]=i mean? I know it adds the value of i into the array, but how? Why couldn't you just write it as result[i] or result[] = i? Similar TutorialsI am using a show/hide script for my sites FAQs. This is the code: However, when you click on one of the Questions it brings you to the top of the page due to the <a href="#" How do I make it so it doesn't bring you to the top of the page? Code: <html> <head> <title>TEST TEMPLATE</title> <script type="text/javascript"> function showHide(id) { var current = document.getElementById(id); for ( var f = 1; f < 99999; ++f ) { var div = document.getElementById("FAQ" + f); if ( div == null ) return; // quit when we run out of FAQs if ( div != current ) { div.style.display = "none"; // zap all others } else { // toggle current one div.style.display = ( div.style.display == "none") ? "block" : "none"; } } return false; // IMPORTANT! } </script> </head> <body> <!------ QUESTIONS ------> <p>Change lines <li> to divs or whatever</p> <li> <a href="#" onclick="return showHide('FAQ1')">FAQ one</a></li> <li> <a href="#" onclick="return showHide('FAQ2')">FAQ two</a></li> <li> <a href="#" onclick="return showHide('FAQ3')">FAQ three</a></li> <!------ ANSWERS ------> <div id="FAQ1" style="display:none;"><p>Ans one</p></div> <div id="FAQ2" style="display:none;"><p>Ans two</p></div> <div id="FAQ3" style="display:none;"><p>Ans three etc etc.</p></div> </body> </html> Hello, I am new to javascript. Below is the code that opens a new window when the user clicks on the button "Click here to see". But the code resides outside the <script></script>. As far as i know javascript codes have to be between these tags to work. How will the "window.open" statement work? Code: <form> <input type="button" value="Click here to see" onclick="window.open('http://www.yahoo.com')" /> </form> I have the following code: Code: if (n==1 && g <=5) { sndPlayer1.URL ="Boy-a1.wav"; } else if (n==1 && g >5) { sndPlayer1.URL ="Girl-a1.wav"; } else if (n==2 && g <=5) { sndPlayer1.URL ="Boy-e1.wav"; } else if (n==2 && g >5) { sndPlayer1.URL ="Girl-e1.wav"; } else if (n==3 && g <=5) { sndPlayer1.URL ="Boy-i1.wav"; } else if (n==3 && g >5) { sndPlayer1.URL ="Girl-i1.wav"; } else if (n==4 && g <=5) { sndPlayer1.URL ="Boy-o1.wav"; } else if (n==4 && g >5) { sndPlayer1.URL ="Girl-o1.wav"; } else if (n==5 && g <=5) { sndPlayer1.URL ="Boy-u1.wav"; } else if (n==5 && g >5) { sndPlayer1.URL ="Girl-u1.wav"; } else if (n==6 && g <=5) { sndPlayer1.URL ="Boy-b1.wav"; } else if (n==6 && g >5) { sndPlayer1.URL ="Girl-b1.wav"; } else if (n==7 && g <=5) { sndPlayer1.URL ="Boy-b1.wav"; } else if (n==7 && g >5) { sndPlayer1.URL ="Girl-b1.wav"; } else if (n==8 && g <=5) { sndPlayer1.URL ="Boy-h1.wav"; } else if (n==8 && g >5) { sndPlayer1.URL ="Girl-h1.wav"; } else if (n==9 && g <=5) { sndPlayer1.URL ="Boy-t1.wav"; } else if (n==9 && g >5) { sndPlayer1.URL ="Girl-t1.wav"; } I was wondering what the best way to make a smaller simpler version of this code is? Would it be using a loop? If so can you help me with how to I have an alert I'm using over in Salesforce to remind sales reps to use a special terms document when quoting. Problem is, the alert is going off for ALL accounts, not just "Account1" and "Account2". Is this line incorrect in syntax or something? if ((isstatus == "Proposal/Price Quote") && (acct == "Account1", "Account2")) Thanks all. This forum has been very helpful for me in the past so this was my first stop! Jeremy Code: <html> <head> <script type="text/javascript" language="javascript" src="/js/functions.js"></script> <script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script> <script type="text/javascript"> function throwalert() { var isstatus = "{!Opportunity.StageName}"; var acct = "{!Opportunity.Account}" var msgg = "Please remember to use a special terms and conditions quote template when quoting this opportunity."; if ((isstatus == "Proposal/Price Quote") && (acct == "Account1", "Account2")) { alert(msgg); } else { window.parent.location.replace = "{URLFOR($Action.Opportunity.View, Opportunity.Id,null,true)};" } } </script> </head> <body onload="throwalert()";> </body> </html> Code: if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailvalue)) this is a code to validate an email address. My question is.. how do you call this type of validation? Those /^\w+([\.-]?\w+.. i don't really understand how they work. Is there a special term used to describe this? I want to learn how it works. I want to search for tutorials online but i cant think of any desriptive word to use as keyword for google search. I cant find helpful results. Please help. Thanks Can someone please tell me why there is an error with my code (see in red below). I am told that 'process' is not an object or is null and I don't understand why or the implications with the click event which I am trying to capture and pass to my function: Javascript Code: Code: var USStates = new Array(50); USStates[0] = "Alabama"; USStates[1] = "Alaska"; USStates[2] = "Arizona"; USStates[3] = "Arkansas"; USStates[4] = "California"; USStates[5] = "Colorado"; USStates[6] = "Connecticut"; USStates[7] = "Delaware"; USStates[8] = "Florida"; USStates[9] = "Georgia"; USStates[10] = "Hawaii"; USStates[11] = "Idaho"; USStates[12] = "Illinois"; USStates[13] = "Indiana"; USStates[14] = "Iowa"; ... var stateEntered = new Array(50); stateEntered[0] = "1819"; stateEntered[1] = "1959"; stateEntered[2] = "1912"; stateEntered[3] = "1836"; stateEntered[4] = "1850"; stateEntered[5] = "1876"; stateEntered[6] = "1788"; stateEntered[7] = "1787"; stateEntered[8] = "1845"; stateEntered[9] = "1788"; ... var process = document.getElementById("search"); process.onclick = getStateDate; function getStateDate() { var selectedState = document.getElementById("entry").value; for (var i = 0; i < USStates.length; i++) { if (USStates[i] == selectedState) { break; } } if (i < USStates.length) { alert(selectedState + " entered the Union in " + stateEntered[i] + "."); } else { alert("Sorry, '" + selectedState + "' isn't a US State."); } } HTML Code ( in case you need it): 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>Loops</title> <script type="text/javascript" src="loops.js"></script> </head> <body> <h2>Enter a US Sate in the field below and click the 'submit' button to get the date of entry in the union</h2> <form action="process.php"> <input type="text" id="entry" name="entry" /> <input type="button" id="search" name="search" value="submit" /> </form> </body> </html> 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) ; I want to Count the number of occurrences of character 't' in a string entered by the user (via prompt) and write the result to the web page. For example, if the user enters "Working with strings", you should write a message saying that the string contains 't' 2 times. I know it has something to do with charAt. Im just playing around with it trying to figure it out and got Code: var str = prompt(); document.write(str.charAt(1)); That doesnt count anything up obviously. i need to use a loop but im not sure how to apply a loop and charAt. Hi everyone, I am a newbie Javascript programmer. I have gotten better lately and am starting creating scripts from scratch to test my knowledge. I wrote the script below and needless to say, it does not work... yet. I know I am not far and I need someone's help to tweak my script and let me know what was wrong with. That would be greatly appreciated. Anyone? I have this div in the middle of a page which has an image. I want the image.src to switch to the next image every 3 seconds. Here's my html code: Code: <body> <div id="slideShow"> <img src="pic3.jpg" id="image" /> </div> </body> Here's my js code: Code: window.onload = initLoad; function initLoad() { // Create a new image object oImage = new Image; // tie the object image object with your target id in your html code oImage = document.getElementById("image"); //Create a new date object var oCurrDate = new Date(); // Create a new time object var oCurrentTime = oCurrDate.getSeconds(); //Loop through all images in the document for(var i=0; i<document.images.length; i++) { //the current image remains so as long as 3 seconds or less have passed while(oCurrentTime <= oCurrentTime + 3) { // if 3 seconds have not passed, your current picture remains on the screen oImage.src = document.images[i].src; } } } Hello guys, Im new to Javascript,please help me ya in my php code, I retrieve a ID value and assign to variable like below PHP Code: $id = $gettingdata['id']; and I want this id to be include together with javascript "insertsubcat" function like below Code: <a href="javascript:insertsubcat('.$ID.');"/>Click to insert this data</a> in the javascript page, I dont know how to call back the id value and i try like code below but it doesnt work Code: function insertsubcat(data) { var data = data.value; . . . . } Any idea? Thanks Azhan Malaysia I'm trying to not allow my users to enter in n/a in my form ex... "N/A" "n/A" "na" My code trips the alert every time no matter what is entered in the cell... Why is it doing that? Code: function appliance() { if (document.ComplianceForm.Appliance.value=="n/a" || "N/A" || "n/A" || "N/a") { alert("You cannot enter any variation of n/a in this cell.") } else { return false; }; } Im new to JavaScript so take it steady, i have an array: <script type="text/javascript"> var colOne [0] = new Array("Copyright", "Council - spending plans - consultation", "Data protection"); var colTwo [1] = new Array("Digital Region", "Freedom of information", "Libraries - online information resources"); var colThree [2] = new Array("Our standards", "Test"}; document.write(colOne[0]); </script> anyone can tell why it does not work? Hello guys, I'm new to this forum and to Javascript. I need to make a very simple website to check whether a ticket number is valid or not. I have created an array with all the codes necessary, but when I try to loop through it, the only thing that works as it should is the first element of the array. Basically, I want the code to display Correct!, if there is a match between user input and array, or Wrong if otherwise. Here is my code: Code: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script type="text/javascript"> function validate() { var list = new Array; list[0]="zDv5BXAc"; list[1]="6YXF3Qwj"; list[2]="k8UKpgwo"; var code= document.getElementById("code"); var name = document.getElementById("name"); for(var i=list.length-1; i>=0; --i) { if (code.value == list[i]) { document.all.confirm.innerHTML = "Correct" } else document.all.confirm.innerHTML = "Wrong" } } </script> </head> <body> <input name="Name" type="text" id="name" size="50"> <input name="Code" type="text" id="code" size="10"> <input name="Check Code" type="submit" value="Verify" onClick="validate()"> <div id="confirm"></div> </body> </html> I'm building a webpage using javascript and iframes. Basically I have an iframe in the middle of the index.html page that links to another html page (let's call it iframe.html). My question is, is it possible to call a javascript function from iframe.html to control an object on index.html? If so, how do I do this? I'd like to be able to assign an image in iframe.html with the hyperlink of href="javascript:function()", where the function effects the CSS of a div on index.html. Thanks Hi there, a website I am trying to load in an Iframe uses this code to ensure that its window is the top window: Code: <script language="JavaScript"> if (window != top) top.location.href = location.href; </script> I was wondering is there any way possible of loading this page in an iframe or is this method of ensuring the page is on top not beatable? Many thanks, David. I'm trying to do some rollover images for a webpage I need to design, for class. I have them set up, and they work.. but the issue is that I set it up in such a way that the .js file is dynamic, and works for each page. You know, so that I don't actually have to specify the images within the .js file. The problem with THAT is that it doesn't automatically load the rollover version of each image, when the page loads.. so it's a bit sluggish. Here's my .js file Code: function swap_image(name,source) { document.images[name].src=source; var argv=swap_image.arguments; if(argv[2] && argv[3] && document.getElementById) { element=document.getElementById(argv[2]); element.innerHTML=argv[3]; } } Here's how I have it in the HTML Code: <a href="gallery" onmouseover="swap_image('gallery','/images/galleryro.jpg')" onmouseout="swap_image('gallery','/images/gallery.jpg')"> <img style = "top:150px; left:100px;" border="0" id="gallery" src="/images/gallery.jpg"/> </a> Is there anything I can do to force it to load the rollover image, without sacrificing my dynamic .js code? Hello, I am trying to create simple Java Script form that will open a URL based on the contents entered into the form. The form shall consist of only a text box and submit button. A user will enter XXXX in the text box and click submit. The form would then load a URL consisting of http://www.company.com/XXXX I know this can be done because I had a code like this a few years back but I have lost it. I've been searching for over an hour, trying to figure this out. Any help would be grately appreciated. Thanks! hey guys i'm very new to javascript and am learning all of it from the internet so i couldn't find any place with what i wanted my problem is: i want to have a variable that if its == to x or y it gives 1 thing and if else its another this is what i got #code# function woo() { ret = prompt("What's Your Name?"); if (ret == 'x','y') { alert ("hey " + ret + ", now try a random name") } else { alert ("No.. " + ret + " isn't your name. Try again") } } #/code# so the problem with that is, it always gives me the first option almost like every answer is the correct one and not just x,y imo the problem is with the line if (ret == 'x','y') so how do i write more than 1 answer to the "if" command? I am very new to javascript and really can't figure out what I am doing wrong. I am trying to make a 1 page store that allows you to type in an item and select the color and size you want for that item. If you don't fill in a field, an alert message should tell you that. If all the fields are filled in, a "thank you for purchasing ____________" message should appear in a text area at the bottom of the page. This is my current code: Code: <html> <head> <script> function processform() { if ( document.form1.item1.value == "") { alert("No Item Chosen") } else { chosen = "" len = document.form1.c1.length for (i = 0; i <len; i++) { if (document.form1.c1[i].checked) { chosen = document.form1.c1[i].value } } if (chosen == "") { alert("No Color Chosen") } } else { itemname = document.form1.item1.value; itemcolor = document.form1.c1.value; itemsize = document.form1.size.value; document.form1.txtarea1.value = "Thank you for purchasing a " + itemsize + " " + itemcolor + " " + itemname; } } </script> </head> <body> <h1>Store</h1> <br/><br/> <form name="form1"> <h4>What item would you like to buy?</h4> <input type="text" name="item1" value="" /><br /> <br/><br/> <h4>Color</h4> <input type="radio" name="c1" value="blue" /> Blue<br /> <input type="radio" name="c1" value="red" /> Red<br /> <input type="radio" name="c1" value="green" /> Green<br /> <input type="radio" name="c1" value="yellow" /> Yellow<br /> <br/><br/> <h4>Size</h4> <select name="size"> <option value="small">Small</option> <option value="medium">Medium</option> <option value="large">Large</option> <option value="extra large">Extra Large</option> </select> <br/><br/> <button onclick="processform()">Purchase</button> <br/><br/> <textarea rows="2" cols="40" name = "txtarea1"> </textarea> </form> </body> </html> When I press the purchase button, no alert message is shown, nor does anything get displayed in the textarea. What am I doing wrong? I have been asking around other forums for this question....no one seems to be able to help. But reading through here you guys seem quite a bit smarter and I'm sure you can point me in the right direction What I need is a simple loading page that is for LOOKS only...the site I am redirecting to is very small and does not take very long to load...but for marketing purposes a loading page will make my business strategy look much more legit. I am linking to an affiliate page with a discount link. The problem is most people aren't actually sure if the discount link works...so I want to make a loading page where it says something like "Activating Discount". SO basically I have: My website>>>(link)>>>HTML Loading Page>>>(Redirects after 5 sec or so)>>>Afilliate Landing Page I'll put a loading wheel on the page like this one: But what I really want it to do is switch the messages its displaying so maybe it says "Loading Site Content" for 1 second then switches to "Activating Discount" for 1 second then switches again to "Validating Referrer" or something like that. THANK you so much for any help you can give me You guys rock! |