JavaScript - Javascript Help To Produce Random Numbers
Hi all.. I've been trying to get random numbers in JS. It works when I assign fixed values, but when I want those values to come from a form, I get all the problems.. any comments on my script? Thanks. M
[CODE] <script type="text/javascript"> function random() { var x=document.getElementById("text1").value;// num ingresado por el usuario var y=document.getElementById("text2").value; //var x=1; //var y=6; num=Math.floor(x + Math.random() * y); var r=document.getElementById("demo") r.innerHTML=num; } </script> [CODE] Similar TutorialsHi, I have this bit of java code that puts a random/unique number in a field on my web page: <html> <head> </head> <body> <form> <input type="text" name="MyField" /> </form> <script langueage="javascript" type="text/javascript"> var d = new Date(); var tm = d.getTime(); document.getElementsByName('MyField')[0].value=tm; </script> </body> </html> My problem is that I want to limit the number to just 6 digits. Is this possible? Any help would be much appreciated Thank you Dave 1st, Hi Coding forums! I'll get to the point, I'm a noob -.- I'm using greasemonkey for a certain website. Basically, I'm using gm to refresh the page after a certain time, I was however wondering, If I can set it to a random number between 2 specific times? The code I'm using atm is setTimeout(function() { document.location.reload(); } , 10000); I know I have to use math.random, well, I think I do. But I'm not sure how to do it between 2 certain times? So to summarise, I'm trying to refresh a page at any given random time between say 5-10 minutes. Any help is appreciated, thanks! hi, My script creates random numbers as shown in ; 2,5,8 1,4,5 8,9,4 5,3,3 1,1,2 2,5,8 5,3,3 ...... ...... but there are many duplications as in row1 and row6(2,5,8.)....row4 and row7 how can we modify the script so that there wont be any smilir lines.. thanks.. here is my script. PHP Code: <script> keywords1 = ["1","4","5","7","2","3"]; keywords2 = ["5", "9", "2", "10","11","6","4"]; keywords3 = ["13", "1", "3", "4", "6","7","8","2"]; var toplam=0; z1=keywords1.length z2=keywords2.length z3=keywords3.length toplam=z1*z2*z3; say=0; zaman=setInterval("yaz()",500); function yaz() { var keyword1 = keywords1[Math.floor(Math.random()*keywords1.length)]; var keyword2 = keywords2[Math.floor(Math.random()*keywords2.length)]; var keyword3 = keywords3[Math.floor(Math.random()*keywords3.length)]; say++; document.getElementById("kutu").innerHTML+=keyword1+".."+keyword2+".."+keyword3; document.getElementById("kutu").innerHTML+="<br>"; document.getElementById("kutu2").value=say; document.getElementById("kutu3").value=toplam; if(say>=toplam) dur(); } function dur() { clearInterval(zaman); } </script> <input type=button value=yazz onclick="yaz()">.. .<input type=button value=dur bakalım onclick="dur()"> ....<input type=text value="" id=kutu2 size=3> ....<input type=text value="" id=kutu3 size=3> <div id="kutu" style="position:absolute;top:5;left:250;background-color:yellow;border-style:solid;border-width:5;border-color:red;"></div> <div id="kutux1" style="position:absolute;top:5;left:350;background-color:yellow;border-style:solid;border-width:5;border-color:red;"></div> </body> </html> I would like to create a Blackjack style game for one of my high school programming classes. I can generate 3 different random numbers and use these random numbers to display an image with that number on it. How do I add the 3 separately generated numbers together to show the total?? Here is my code so far. Specifically I would like to add the myNumber1(), myNumber2() and myNumber3() results together. <html> <head> <title>Play 21</title> <script language="javascript"> function myNumber1(){ var rand_no = Math.random(); var myTotal; var ComputerTotal; rand_no = 5 * rand_no + 1; rand_no = Math.floor(rand_no); document.pic1.src = rand_no + ".gif" myNumber2(); myNumber3(); myNumber1D(); myNumber2D(); myNumber3D(); } </script> <script language="javascript"> function myNumber2(){ var rand_no2 = Math.random(); rand_no2 = 5 * rand_no2 + 1; rand_no2 = Math.floor(rand_no2); document.pic2.src = rand_no2 + ".gif" } </script> <script language="javascript"> function myNumber3(){ var rand_no3 = Math.random(); rand_no3 = 5 * rand_no3 + 1; rand_no3 = Math.floor(rand_no3); document.pic3.src = rand_no3 + ".gif" } </script> <script language="javascript"> function myNumber1D(){ var rand_no1D = Math.random(); rand_no1D = 5 * rand_no1D + 1; rand_no1D = Math.floor(rand_no1D); document.pic1D.src = rand_no1D + "D.gif" } </script> <script language="javascript"> function myNumber2D(){ var rand_no2D = Math.random(); rand_no2D = 5 * rand_no2D + 1; rand_no2D = Math.floor(rand_no2D); document.pic2D.src = rand_no2D + "D.gif" } </script> <script language="javascript"> function myNumber3D(){ var rand_no3D = Math.random(); rand_no3D = 5 * rand_no3D + 1; rand_no3D = Math.floor(rand_no3D); document.pic3D.src = rand_no3D + "D.gif" } </script> </head> <body> <table width="94%" border="1"> <tr> <td width="39%"><div align="center">First Number <img src ="1.gif" name="pic1" id=1/> </div></td> <td width="32%"><div align="center">Second Number <img src ="2.gif" name=pic2 id=2/></div></td> <td width="14%"><div align="center">Third Number <img src ="3.gif" name=pic3 id=3/> </div></td> <td width="15%"> </td> </tr> <tr> <td width="39%"><div align="center">First Number <img src ="1D.gif" name="pic1D" id=1/> </div></td> <td width="32%"><div align="center">Second Number <img src ="2D.gif" name=pic2D id=2/></div></td> <td width="14%"><div align="center">Third Number <img src ="3D.gif" name=pic3D id=3/> </div></td> <td width="15%"> </td> </tr> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </td> </tr> <tr> <td colspan="4"><div align="center"> <input type="submit" name="Deal_Cards" id="Deal_Cards" value="Deal Cards" onClick="myNumber1()" /> </div></td> </tr> </table> </body> </html> jchudson jchudson@mts.net Hello all, I hope someone can help me with this. I have a form wich has a add/remove row button, this so the visitor can select multiple sets of data. All is fine and is being submitted via PHP. Only problem is from number 10 and up, i am getting strange random numbers in the new rows that are added in their name. Like 1111 (instead of 11), 122222 (instead of 12). And because of this, every row from 10 and up won't be send through php, giving this random effect. I can't seem to find why this is happening. Hopefully someone can help me out in this. The full form can be viewed http://www.multisearch.info/test/form_ruby_2.html The code i use for my Clone Element is Code: //clone element var clone; function cloneRow(row){ clone=row.cloneNode(true); } function addRowToTable(row){ var tbody=row.parentNode; var inputs=clone.getElementsByTagName('input'), i=0, inp; i=0; while(inp=inputs[i++]){ if(inp.type=='text'){ inp.onfocus=function(){this.value='';} } if(inp.type=='button'){ inp.value=='Add'?inp.onclick=function(){addRowToTable(this.parentNode.parentNode)}:inp.onclick=function(){removeRowFromTable(this.parentNode.parentNode)}; } } tbody.appendChild(clone); cloneRow(tbody.getElementsByTagName('tr')[tbody.getElementsByTagName('tr').length-1]); reorderNames(); } function removeRowFromTable(row){ row.parentNode.removeChild(row); reorderNames(); } function reorderNames(){ var rows=document.getElementById('tblSample').getElementsByTagName('tbody')[0].getElementsByTagName('tr'), r, i=0, el, e, j; while(r=rows[i++]){ el=r.getElementsByTagName('*'); j=0; while(e=el[j++]){ e.nodeName=='SELECT'||(e.nodeName=='INPUT'&&e.type=='text')?e.name=e.name.replace(/\d/,i):null; } } } onload=function(){ var row=document.getElementById('tblSample').getElementsByTagName('tr')[0]; cloneRow(row); } i have a table with a couple random numbers, and i want to click a button that will dynamically regenerate the numbers each time the button is clicked without re-generating the entire table., im using DOM and innerhtml for these random numbers. heres the javascript and html code. so far, it just generates the random numbers when the page loads. var random = Math.floor(Math.random()*40 + 1) //sets variables for random numbers to generate var random2 = Math.floor(Math.random()*40 + 1) var random3 = Math.floor(Math.random()*40 + 1) var random4 = Math.floor(Math.random()*40 + 1) var random5 = Math.floor(Math.random()*40 + 1) var random6 = Math.floor(Math.random()*40 + 1) //create table function makeTable(lotto) document.getElementById("tableSpan").innerHTML = '<table border="1" id="lotto">'; var caption=document.getElementById('lotto').createCaption(); caption.innerHTML="JavaScript Random Numbers"; var x=document.getElementById('lotto').insertRow(0); var cell1=x.insertCell(0); var cell2=x.insertCell(1); var cell3=x.insertCell(2); var cell4=x.insertCell(3); var cell5=x.insertCell(4); var cell6=x.insertCell(5); cell1.innerHTML='<td class = "normal">'+ random +'</td>'; cell2.innerHTML='<td class = "normal">'+ random2 +'</td>'; cell3.innerHTML='<td class = "normal">'+ random3 +'</td>'; cell4.innerHTML='<td class = "normal">'+ random4 +'</td>'; cell5.innerHTML='<td class = "normal">'+ random5 +'</td>'; cell6.innerHTML='<td class = "red">'+ random6 +'</td>'; } heres the HTML file: <body onload="makeTable('lotto');"> <div id="container"> <div id="header"> <h1>Welcome</h1> </div> <div id="content"> <span id="tableSpan"></span> <input type="button" value="Re-generate Numbers" onclick="makeTable('lotto');" /> </div> { Hello! I am trying to create a javascript code in which I prompt the user for a number (an integer from 0 to 100) to search for, then search a function with the number they entered and then display whether the number was found, and if found, a location where it can be found within the list. Can you please tell me what is wrong? <html> <head> </head> <body> <script language = "javascript"> var list = new Array(200); for (var i=0; i < 200; i++){ list=Math.round(Math.random()*100) + " "; document.write(list); } var value=prompt("What is the number you are searching for? ", " "); if (list[i]==value){ {alert("Number has been found in location" +i)}; } </script> </body> </html> Thank you I am creating a calculator app and I'm a bit stuck with number formatting. Example of how I would like a number to be formatted: $2,899,799 I currently have the formatting working except for the commas. I'm using the following code to achieve this. My result is currently showing up like this $2899799 Code: function format (expr, decplaces) { var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces)) while (str.length <= decplaces) { str = "0" + str } var decpoint = str.length - decplaces return str.substring(0,decpoint) + str.substring(decpoint,str.length); } function dollarize (expr) { return "$" + format(expr,0) //RESULT FIELDS form.AnnualScrapSales.value = dollarize(((A - B) * (C2/100)) * G2) is there a way to add to this "function format" code to also achieve the commas? I am using Javascript validation, for all my HTML control that are not run at server... Now i need to allow the user to enter only numbers in a textbox..If any of them apply characters alert should come.. How can i do that in javascript.... This is the program.and here is the script. I have tried all I know without success. This Sources program is on another css/html forum for another issue. Frank Code: //** Dynamic Drive Equal Columns Height script v1.01 (Nov 2nd, 06) //** http://www.dynamicdrive.com/style/blog/entry/css-equal-columns-height-script/ var ddequalcolumns=new Object() //Input IDs (id attr) of columns to equalize. Script will check if each corresponding column actually exists: ddequalcolumns.columnswatch=["Content1", "content2", "content3",] ddequalcolumns.setHeights=function(reset){ var tallest=0 var resetit=(typeof reset=="string")? true : false for (var i=0; i<this.columnswatch.length; i++){ if (document.getElementById(this.columnswatch[i])!=null){ if (resetit) document.getElementById(this.columnswatch[i]).style.height="auto" if (document.getElementById(this.columnswatch[i]).offsetHeight>tallest) tallest=document.getElementById(this.columnswatch[i]).offsetHeight } } if (tallest>0){ for (var i=0; i<this.columnswatch.length; i++){ if (document.getElementById(this.columnswatch[i])!=null) document.getElementById(this.columnswatch[i]).style.height=tallest+"px" } } } ddequalcolumns.resetHeights=function(){ this.setHeights("reset") } ddequalcolumns.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload) var tasktype=(window.addEventListener)? tasktype : "on"+tasktype if (target.addEventListener) target.addEventListener(tasktype, functionref, false) else if (target.attachEvent) target.attachEvent(tasktype, functionref) } ddequalcolumns.dotask(window, function(){ddequalcolumns.setHeights()}, "load") ddequalcolumns.dotask(window, function(){if (typeof ddequalcolumns.timer!="undefined") clearTimeout(ddequalcolumns.timer); ddequalcolumns.timer=setTimeout("ddequalcolumns.resetHeights()", 200)}, "resize") I have no js experience or coding experience of any kind other than the last few hours of reading a book called 'object-oriented javascript', and all was well until loops. firstly, this is how he teaches the while loop... var i = 0; while (i <10) {i++;} which results in 9 we move on to for loops... var res =''; for( var i = 0; i <10; i++) {res += 'sometext' ;} which repeats, but I'd like to be able to make that into a sequence of numbers... 123456789. how do I do that? thanks. Hi, I have got a code here for lottery numbers, it has been asked to matches the draw number and it is not working, a help really needed from any kindness person. I am new to JavaScript, and I have been asked to complete the codes: <html> //Variables //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- var draw = new Array; //Variable to use in Array to store entered draw numbers. var bb=0; //Variable for bonus ball to use in the bonus ball opening slash screen. var vbb=0; //Variable for valid bonus ball to be used in the bonus ball opening slash screen. var c=0; var date = new Date(); //Variable for time and date. //Syndicate numbers in Array //-------------------------------------------------------------------------------------------------------------------------------------------------------------- var set1 = new Array(); //Variable for set 1 syndicate numbers to use in Array. set1[0]=" 3"; //Number to be used in (set1) Array. set1[1]=" 9"; //Number to be used in (set1) Array. set1[2]=" 18"; //Number to be used in (set1) Array. set1[3]=" 27"; //Number to be used in (set1) Array. set1[4]=" 30"; //Number to be used in (set1) Array. set1[5]=" 33"; //Number to be used in (set1) Array. var set2 = new Array(); //Variable for set 2 syndicate numbers to use in Array. set2[0]=" 5"; //Number to be used in (set2) Array. set2[1]=" 7"; //Number to be used in (set2) Array. set2[2]=" 11"; //Number to be used in (set2) Array. set2[3]=" 12"; //Number to be used in (set2) Array. set2[4]=" 34"; //Number to be used in (set2) Array. set2[5]=" 42"; //Number to be used in (set2) Array. var set3 = new Array(); //Variable for set 3 syndicate numbers to use in Array. set3[0]=" 4"; //Number to be used in (set3) Array. set3[1]=" 15"; //Number to be used in (set3) Array. set3[2]=" 21"; //Number to be used in (set3) Array. set3[3]=" 23"; //Number to be used in (set3) Array. set3[4]=" 27"; //Number to be used in (set3) Array. set3[5]=" 31"; //Number to be used in (set3) Array. var syndicates = new Array(); //Variable to use in Array for syndicate numbers. syndicates[0]=set1; //String to use in Array for syndicate numbers. syndicates[1]=set2; //String to use in Array for syndicate numbers. syndicates[2]=set3; //String to use in Array for syndicate numbers. </script> </head> <body> <script> //The Opening ("Splash") screen to ask user for the six numbers and the bonus ball from number 1 to 49 //------------------------------------------------------------------------------------ for (counter=1; counter < 6; counter++) { //Opening brace on new line. do { //Opening brace on new line. dns=window.prompt("Please Enter Draw Number From 1 to 49 : ", counter); if (isNaN(dns) == true) dns=0; } while ((dns < 1) || (dns > 49)); var vd=draw.indexOf(dns); //Local variable to use in entering draw numbers for returning the position of the occurrence of the draw value as a string. if (vd >= 0) //If the value greater than 0, do... . { //Opening brace on new line. alert("Duplicate Number Entered!"); //Alert pop-up. counter--; // } else draw[counter]=dns; } while (vbb < 1) { do { bb=window.prompt("Please Enter Bonus Ball From 1 to 49 : ", counter); if (isNaN(bb) == true) bb=0; } while ((bb < 1) || (bb > 49)); var vd=draw.indexOf(bb); //Local variable to use in entering the bonus ball for returning the position of the occurrence of the bonus ball value as a string. if (vd >= 0) alert("Duplicate Number Entered!"); else vbb=1; } //Background colour and to display title, time and date //---------------------------------------------------------------------------------------------------------------------------------- document.body.style.background = "#FFB6C1"; document.write("<h2><u><p align=center>Lottery Number Checker</p></u></h2>"); document.write("<b>Time: </b>"); document.write(date.getHours()); document.write("."); document.write((date.getMinutes())); document.write("<br><b> Date: </b>"); document.write(date.getDate()); document.write("."); document.write(date.getMonth() + 1); document.write("."); document.write((date.getFullYear()), "<br><br><br>"); //Entered numbers, draw numbers, bonus ball, number of matches //---------------------------------------------------------------------------------------------------------------------------- document.write("<b>The numbers you have entered for the draw and bonus ball a </b>",(draw),", ",(bb),".<br><br><br>"); document.write("<b>Draw numbers from set 1 a </b>",(set1),"<br>"); document.write("<b>Draw numbers from set 2 a </b>",(set2),"<br>"); document.write("<b>Draw numbers from set 3 a </b>",(set3),"<br><br><br>"); displaymatches(); document.write("<br><br>"); document.write((bbcheck()),"<br><br><br>"); //Function to check draw and bonus ball against entries numbers //----------------------------------------------------------------------------------------------------------------------------------------------------- function drawcheck(dnums) { for (a=0; a < 6; a++) for (b=c; b < 6; b++) if (syndicates[a]=draw[b]); { document.write(dnums[a]," "); c=b; } } //Function for syndicate numbers hit //-------------------------------------------------------------------------------------------------------------------------------------------------- function hits(dnums) { for (d=0; d < 6; d++) for (e=c; e < 6; e++) if (syndicates[d] == draw[e]) { noh++; c=e; } return noh; } //Function to display matches//-------------------------------------------------------------------------------------------------------------------------------------------------- function displaymatches() { var times=syndicates.length; for (f=0; f < times; f++) { if (noh > 0) { document.write("<b>The number of hits for set ",f+1," is: </b>",noh,"<br>"); document.write("<b>Matching numbers for set ",f+1," are : </b>"); drawcheck(syndicates[f]); } else document.write("No hits for set ",f+1," this time <br>"); for (g=0; g < 6; g++) if (bb == syndicates[f][g]) document.write("<b><font color=blue>Matchig bonusball is: </b>",bb,"</font><br>"); document.write("<br><br>"); } } </script> </body> </html> I need to make a --very-- simple version of this game: http://javascript.internet.com/games/magic-squares.html. You need to click on the buttons until they're in order, then you win. I'm having trouble with the function that trades the places of the two buttons. So, can anyone tell me how my "swap" function should be? It needs to test if the value of the button next to the button clicked on is blank, and then if it is, the two values trade places. This way, I will hopefully end up with 9 different functions, depending on which square I'm clicking on, because the "id" of the button stays the same, it just swaps the values. It shouldn't use any arrays or anything. Thanks, I appreciate the help! Here's what I have so far: Code: <html> <head> <title>15 Puzzle</title> <script type="text/javascript"> function swap(id){ if(document.getElementById(id+1).value="") { document.getElementById(id+1).value=document.getElementById(id).value; document.getElementById(id).value=""; alert('It works!'); } } </script> </head> <p><h1 align="center">15 Puzzle</h1><p> <p align="center"><input type="button" id="scramble" value="Scramble"></p> <br> <table border="3" bordercolor="black" width="300" bgcolor="" cellpadding="50" align="center"> <tr> <td ><input type="button" id="1" value="1" onclick=" swap(this.id);"></td> <td > <input type="button" id="2" value="" onclick="swap(this.id)"></td> <td> <input type="button" id="3" value="3" onclick="swap(this.id)"> </td> <td> <input type="button" id="4" value="4" onclick="swap(this.id)"> </td> </tr> <tr> <td> <input type="button" id="5" value="5" onclick="swap(this.id)"> </td> <td> <input type="button" id="6" value="6" onclick="swap(this.id)"> </td> <td> <input type="button" id="7" value="7" onclick="swap(this.id)"> </td> <td> <input type="button" id="8" value="8" onclick="swap(this.id)"> </td> </tr> <tr> <td> <input type="button" id="9" value="9" onclick="swap(this.id)"> </td> <td> <input type="button" id="10" value="10" onclick="swap(this.id)"> </td> <td> <input type="button" id="11" value="11" onclick="swap(this.id)"> </td> <td> <input type="button" id="12" value="12" onclick="swap(this.id)"> </td> </tr> <tr> <td> <input type="button" id="13" value="13" onclick="swap(this.id)"> </td> <td> <input type="button" id="14" value="14" onclick="swap(this.id)"> </td> <td> <input type="button" id="15" value="15" onclick="swap(this.id)"> </td> <td> <input type="button" id="16" value="2" onclick="swap(this.id)"> </td> </tr> </table> </html> Box 1 - Box 2 / Box 3 and i put a round off number so the decimals dont give a million numbers the problem is...the math is wrong now.... someone please help me fix this.. <!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> <title>Calc</title> <script type="text/javascript"> function calculate() { var one = document.getElementById("one").value*1; var two = document.getElementById("two").value*1; var three = document.getElementById("three").value*1; document.getElementById("answer").value=((one-two)/three); ; } </script> </head> <body> Enter Number 1 <br> <br><input type="text" id="one" /><hr>Enter Number 2<br><br><input type="text" id="two" /><br><br><hr>Enter Average number<br><br><input type="text" id="three" /><br><br><button onclick="calculate();">Calculate</button> <br id="result"></center></br> Total <input type="text" readonly="readonly" id="answer" /><br><br></center> </body> </html> </td> </tr> </table> </center> </body> </html> Hello! I have a really simple JavaScript calculator I'm running, and for the life of me can not figure out how to solve this crazy number problem. It's not doing the math properly, and javascript is not my strongest suit. All my script does is take user input of numbers into a form field, subtract that from another form and multiply the answer of those two forms by whatever the user put in. Example: 210 (minus) 120 (multiplied by) .90 = 81 Here is the actual script: Code: // Calculator function CalculateSum(Atext, Btext, Ctext, form) { var A = parseFloat(Atext); var B = parseFloat(Btext); var C = parseFloat(Ctext); form.Answer.value = A - B * C; } /* ClearForm: this function has 1 argument: form. It clears the input and answer fields on the form. It needs to know the names of the INPUT elements in order to do this. */ function ClearForm(form) { form.input_A.value = ""; form.input_B.value = ""; form.input_C.value = ""; form.Answer.value = ""; } // end of JavaScript functions --> And the html I am using: Code: <form name="Calculator" method="post"> <p>Base Average<input type=text name="input_A"></p> <p>Current Average:<input type=text name="input_B"></p> <p>Percentage of:<input type=text name="input_C"></p> <p>Your ball speed is: (miles per hour) <input name="Answer" type=text readonly> </p> <p> <input type="button" value="Calculate Handicap" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"> <input type="button" value="Clear" name="ClearButton" onClick="ClearForm(this.form)"> </p> </form> Any help would be greatly appreciated! Hi all, I having a problem. I am having 10 images, and I three places on a webpage where I want to show 3 out of those 10 images randomly. But when for example image 5 is shown on spot 1, it cannot be shown on spot 2 or 3 and the same for spot 2 and 3. Is this possible with Javascript? Greetz, Bob i have a table with a couple random numbers, and i want to click a button that will dynamically regenerate the numbers each time the button is clicked without re-generating the entire table., im using DOM and innerhtml for these random numbers. heres the javascript and html code. so far, it just generates the random numbers when the page loads. i posted this problem a while back, and the answers were great, but they coded the xhtml inside the html file. i'm trying to get the numbers to generate in the table that is formed in the javascript file using innerhtml. i tried to incorporate the solutions without putting the html inside the html file to no avail. var random = Math.floor(Math.random()*40 + 1) //sets variables for random numbers to generate var random2 = Math.floor(Math.random()*40 + 1) var random3 = Math.floor(Math.random()*40 + 1) var random4 = Math.floor(Math.random()*40 + 1) var random5 = Math.floor(Math.random()*40 + 1) var random6 = Math.floor(Math.random()*40 + 1) //create table function makeTable(lotto) document.getElementById("tableSpan").innerHTML = '<table border="1" id="lotto">'; var caption=document.getElementById('lotto').createCaption(); caption.innerHTML="JavaScript Random Numbers"; var x=document.getElementById('lotto').insertRow(0); var cell1=x.insertCell(0); var cell2=x.insertCell(1); var cell3=x.insertCell(2); var cell4=x.insertCell(3); var cell5=x.insertCell(4); var cell6=x.insertCell(5); cell1.innerHTML='<td class = "normal">'+ random +'</td>'; cell2.innerHTML='<td class = "normal">'+ random2 +'</td>'; cell3.innerHTML='<td class = "normal">'+ random3 +'</td>'; cell4.innerHTML='<td class = "normal">'+ random4 +'</td>'; cell5.innerHTML='<td class = "normal">'+ random5 +'</td>'; cell6.innerHTML='<td class = "red">'+ random6 +'</td>'; } heres the HTML file: <body onload="makeTable('lotto');"> <div id="container"> <div id="header"> <h1>Welcome</h1> </div> <div id="content"> <span id="tableSpan"></span> <input type="button" value="Re-generate Numbers" onclick="makeTable('lotto');" /> </div> { ***I need to randomise the order of some questions that are in this format. Any ideas? Thank you!*** <CODE> <!---Question4---> <form name="form4" method="post" action=""> <h3><p>First One</p></h3> <p> <label><input type="radio" name="q4" id="w1" value="1" </label> <label><input type="radio" name="q4" id="w2" value="2" </label> <label><input type="radio" name="q4" id="w3" value="2" </label> </p> </form </CODE> Hi All. I am new to all this and I am posting a problem up here that I think is quite rare or has not been looked into for a long time now. I am basically looking to create a background for my website and when you refresh the page the image changes to another picture for the background. I have asked one of my good friends to have a look at it and he gave me some code to work with and so I have changed bits here and there and it worked in firefox and safari but will not work in IE. When I asked him he had no clue what to do. So here I am now! I am hoping it is simple but I also know it wont be to an extent! Any help would be much appreciated here. I have attached the code here for you and also the html file. 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> <style type="text/css"> <!-- #myFlash { position:absolute; width:100%; height:100%; margin:0; padding:0; left:0; right:0; z-index:20; } --> </style> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Experts Connect</title> <meta name="keywords" content="Experts connect Preston, Experts connect Lancashire, Medical reports, Preston medical and legal agency, Lancashire medical and legal agency, specialist providers of independent medical evidence, diagnostic and rehabilitation services to the personal injury market, Medico Legal, personal injury claims, refferers." /> <meta name="description" content="Experts Connect are an established legal and medical agency providing reports for persobal injury claims. We work to get the best possible examination we can to ensure your claim goes through with no fuss. We are based nationally and work all over the United Kingdom." /> <meta name="robots" content="INDEX,FOLLOW" /> <meta name="revisit-after" content="10 days" /> <link rel="stylesheet" type="text/css" href="/common/basic.css" /> <script type="text/javascript" src="crossfade.js"></script> <script type="text/javascript" src="swfobject.js"></script> <script type="text/javascript"> swfobject.registerObject("myFlashContent", "9.0.0", "expressInstall.swf"); </script> <style type="text/css"> body { width:auto; height:auto; background-color:#CCCCCC;} #bgimage { filter:alpha(opacity=40); -moz-opacity:0.40; -khtml-opacity:0.40; opacity:0.40; width:100%; height:100%; position:absolute; margin:0; padding:0; left:0; right:0; } </style> <title>Untitled Document</title> <SCRIPT LANGUAGE="JavaScript"> <SCRIPT/> <!-- Begin // Set up the image files to be used. var theImages = new Array() // do not change this // To add more image files, continue with the // pattern below, adding to the array. theImages[0] = 'back4.jpg' theImages[1] = 'back2.jpg' theImages[2] = 'back5.jpg' theImages[3] = 'back6.jpg' theImages[4] = 'back8.jpg' // do not edit anything below this line var j = 0 var p = theImages.length; var preBuffer = new Array() for (i = 0; i < p; i++){ preBuffer[i] = new Image() preBuffer[i].src = theImages[i] } var whichImage = Math.round(Math.random()*(p-1)); function showImage(){ document.write('<img id="bgimage" src="'+theImages[whichImage]+'">'); // End --> </script> </head> <div id="myFlash"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="myFlashContent" name="expertsconnect" align="middle"> <param name="movie" value="experts-connectwebsite.swf" /> <param name="quality" value="high" /> <param name="scale" value="exactfit" /> <param name="wmode" value="transparent" /> <param name="allowfullscreen" value="true" /> <param name="allowscriptaccess" value="always" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="experts-connectwebsite.swf" width="100%" height="100%" align="middle"> <param name="quality" value="high" /> <param name="scale" value="exactfit" /> <param name="wmode" value="transparent" /> <param name="allowfullscreen" value="true" /> <param name="allowscriptaccess" value="always" /> <!--<![endif]--> <a href="http://www.adobe.com/go/getflashplayer"> <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /> </a> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin showImage(); // End --> </script> <!-- Script Size: 1.49 KB --> </body> </html> There is also a flash file embedded into this document! Any help would be great with this. Thank you, Mark Hi everyone, I'd really appreciate some help, I'm trying to make the menu of my website randomly change. I have 6 different image folders in which there are 7 pictures which all read the same things (menu, drawings, photo's etc) but in different writing. I want the website to (when refreshed, or when somebody clicks on one of the menu buttons) randomly pick a folder each time, and use the images of that folder. So the images are all the same name, just the folder needs changing. This is what I have so far (thanks to another forum aswell), which doesn't work. I'm very new to javascript... I can't really 'read' what is says, unlike html and/or css. I 'get' html and css. This really frustrates me. Help?!?! btw: I took everything out that doesn't concern this problem, for easier reading. Code: <html> <head> <title></title> <link type="text/css" rel="stylesheet" href="css/desktop.css" media="screen" /> <script type="text/javascript" src="js/prototype.js"></script> <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script> <script type="text/javascript" src="js/lightbox.js"></script> <script type="text/javascript"> function randomSort(a,b) { return( parseInt( Math.random()*6 ) %2 ); } var baseURL = 'http://www.SITE.nl/'; var mijnmenu = ['images/pl2/menubalpen','images/pl2/menugeo','images/pl2/menuinkt','images/pl2/menukleur','images/pl2/menupotlood','images/pl2/menusephia']; function randomizeMenu() { var rndMenu = mijnmenu.sort(randomSort); var menuObj= document.getElementById('menu').getElementsByTagName('img'); for (var i=0; i<menuObj.length; i++) { menuObj[i].src = baseURL+'/'+rndMenu[i]+'img'; } } </script> </head> <body onload="randomizeMenu()"> <div id="wrapper"> <div id="menu"> <a href="index.html"><img src="menu1.jpg" /></a> <a href="drawings.html"><img src="menu2.jpg" /></a> <a href="typo.html"><img src="menu3.jpg" /></a> <a href="photo.html"><img src="menu4.jpg" /></a> <a href="movies.html"><img src="menu5.jpg" /></a> <a href="other.html"><img src="menu6.jpg" /></a> <a href="contact.html"><img src="menu7.jpg" /></a> </div> <div id="largecolumn"> TEXT </div> </div> </body> </html> |