JavaScript - Return Random Hash Table Entry
Having trouble figuring out how to do this...I can get the particular entry by name but I can't randomly select one from the several because it's a string and not a number....
Code: var newOre = 0; var ore = { "iron" : 5, "silver" : 3, "gold" : 2, "platinum" : 1 } var userOre = { "iron" : 0, "silver" : 0, "gold" : 0, "platinum" : 0 } Code: function getAnotherOre () { newOre = ore[Math.floor(Math.random() * ore.length)]; userOre += newOre; } The random() is for an array, tried to use it here and returns 0 every time because the key in the key:value pair is a "string", though if I'm reading it right, it is trying to get a random number based off the length of the table, so why doesn't it do that? Ideally I want it to increment a corresponding hash table of users Ores...so if you randomly get a iron, it adds 5 to the users "iron" entry. I had a for loop, but I only need to randomly generate 1 entry result each time called so do I need to loop through things or can I just randomly get a key:value pair back and append that to the userOre table??????? AM I APPROACHING THIS WRONG FOR WHAT I WANT TO DO? Any ideas or link to documentation? I'm searching..... I can get the individual value of a field manually: console.log("keyName"); which will output the right number, but how do I get a random("keyName") and once I figure that out I probably would append("correspondingName", value) to the other table... Basically, functionally I just want to randomly get one of several values when the function is called. I guess I could make global variables for each type of ore, then just manually increment a random one some other way... This is for a html5 game where when you collide with a "mine" it triggers the function but I want it to randomly give various types of ore instead...Maybe I'm going about it wrong... Maybe it's easier just to declare 10 different global variables (assuming 10 ore types) and randomly give a number 0-10 of each every time the function is called......? Similar TutorialsI'm a newby that just finished an online javascript course. When I do a search on Math.random() I receive sites that all describe the command as returning a number 'between 0 and 1'. My question is - are the expected numbers limited to - 0.0000000000000001 thru 0.9999999999999999 or is it possible to get the 0 and 1 also. Seems like the definition should be 'from 0 thru 1' rather than 'between 0 and 1'. JoeF <script language="javascript"> function splitNumber() {alert("inside splitnumber"); var x = parseInt(document.getElementById("value1").value); var z = x%10; /*units*/ var y = (x - z)/10; /*tens*/ var c = parseInt(document.getElementById("value3").value); var a = c%10; /*units*/ var b = (c - a)/10; /*tens*/ for(var i=0;i<=z;i++) { var parseInt(document.getElementById("u1").td.value); for(((document.getElementById("u1").td.value))=0;((document.getElementById("u1").td.value))<=i;((doc ument.getElementById("u1").td.value))++) { document.write("<td> " +document.getElementById("u1").td.value +"</td>") } } } </script> /*this is the code i write for generating unique values in the table cells, but it is not working*/ <body > <div class="left"> <table class="main" cellpadding="3px" cellspacing="6px"> <tr> <td width="630px" height="50px" bgcolor="#ffffff"> <table class="tens"> <tr id="t1"> <td value="9"></td> <td value="8"></td> <td value="7"></td> <td value="6"></td> <td value="5"></td> <td value="4"></td> <td value="3"></td> <td value="2"></td> <td value="1"></td> </tr> </table> </td> <td width="200px" height="50px" bgcolor="#ffffff"> <table class="units"> <tr id="u1"> <td value="9"></td> <td value="8"></td> <td value="7"></td> <td value="6"></td> <td value="5"></td> <td value="4"></td> <td value="3"></td> <td value="2"></td> <td value="1"></td> </tr> </table> </td> </tr> <tr> <td width="630px" height="50px" bgcolor="#ffffff"> <table class="tens"> <tr id="t2"> <td value="9"></td> <td value="8"></td> <td value="7"></td> <td value="6"></td> <td value="5"></td> <td value="4"></td> <td value="3"></td> <td value="2"></td> <td value="1"></td> </tr> </table> </td> <td width="200px" height="50px" bgcolor="#ffffff"> <table class="units"> <tr id="u2"> <td value="9"></td> <td value="8"></td> <td value="7"></td> <td value="6"></td> <td value="5"></td> <td value="4"></td> <td value="3"></td> <td value="2"></td> <td value="1"></td> </tr> </table> </td> </tr> <tr> <td width="630px" height="50px" bgcolor="#ffffff"> <table class="tens"> <tr id="t3"> <td value="9">9</td> <td value="8">8</td> <td value="7">7</td> <td value="6">6</td> <td value="5">5</td> <td value="4">4</td> <td value="3">3</td> <td value="2">2</td> <td value="1">1</td> </tr> </table> </td> <td width="200px" height="50px" bgcolor="#ffffff"> <table class="units"> <tr id="u3"> <td value="9">9</td> <td value="8">8</td> <td value="7">7</td> <td value="6">6</td> <td value="5">5</td> <td value="4">4</td> <td value="3">3</td> <td value="2">2</td> <td value="1">1</td> </tr> </table> </td> </tr> </table> </div> <div class="right" > <table cellpadding="3px"> <tr> <td width="5px"></td> <td><span class="row"><input min="0" max="99" type="number" id="value1" name="value1" value="0" onchange="javascript:addNumbers()"/></span></td> </tr> <tr> <td width="5px"><b style="font-size:25px; position:relative; top:18px;">+</b></td> <td><span class="row"><input min="0" max="99" type="number" id="value3" name="value3" value="0" onchange="javascript:addNumbers()"/></span></td></tr> <tr><td width="5px"></td> <td><span class="row" ><input style=" width:66px; position:relative; right:25px;" readonly="readonly" id="answer" name="answer" value="0" /></span></td></tr> </table> </div> </body> Hi All, I have a button in my html form that will process some functions when user clicks on the button. The problem is after processing the functions, the result is not displayed in the form where I want it to be displayed. I want to ask whether we can create table in the function and display the result in the table row/column but in the same form. Is this possible to be done? And how to do this? In this form cpiM, the input button will call function showIndex. Code: <tr> <td><input type="button" value="Enter" onclick="showIndex(document.cpiM.currFrom.options.selectedIndex, document.cpiM.currTo.options.selectedIndex, document.cpiM.base.options.selectedIndex, document.cpiM.country.options.selectedIndex)"> </td> <td><input type="button" onclick="frmResetM()" value="Reset form" /> </td> </tr> In this function, I want to display the result of calcIndex right below the button Enter in the form cpiM. Code: function showIndex(frm, to, base, country) { for (i=frm; i<=to; i++) { document.write(calcIndex(i, base, country)); document.write("<br/>"); } } I am working on a work project that requires a 5*5 table, that will generate random letters in each cell upon button click. I have most of the coding complete with the exception of the letter generation. I am currently generating numbers, I would even be fine with having a RandBetween(and listing the 26 letters of the alphabet) at this point. Any suggestions? Code: <script type="text/javascript"> var random1, random2, random3, random4, random5, random6, random7, random8, random9, random10, random11, random12, random13, random14, random15, random16, random17, random18, random19, random20, random21, random22, random23, random24, random25; var randomArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; //generate the random characters function CreateCellData() { for (var i=0; i<randomArray.length; i++) { randomArray[i] = Math.floor(Math.random()*99 + 1); } } 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> { I don't understand the logic of Break, Return False, Return True. It was never really covered in our college class, and I see everyone using it. I got an A in the class, if that 'proves' that I really tried to apply myself. NOTE: I understand what the function is doing. I just don't understand WHEN to use break, return false or return true if the the translator can determine the conditional statements. PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } else return false; } if the the translator can determine the conditional statements, why not write it like this: PHP Code: function submitForm(){ var ageSelected = false; for (var i=0; i<5; ++1){ if (document.forms[0].ageGroup[i].checked == true) { ageSelected = true; break; // what's the point for the 'break'? Won't the rest of the code be ignored since it passed the first condition? } } if (ageSelected == false){ window.alert("You must select your age group"); return false; } // why not leave the last else out? is it just a 'safety' catch, in case something other than true or false is inputted? else return false; // what's the point? } Questions: Why use return true, if the translator knows it's ture? Why use "return false" if the translator knows it's false and the alert window has already gone up? why not use "break" to stop the code? Why use the "return false" at the end "else" statement? Hi room, Hey, I opened up the source code for this page in google chrome and since i'm learning javascript, i wanted see if i could "read" it and figure out what was going on. I'm am having the hardest time understanding "return false" and "return true". Could someone step me through this via interpreting this code (in bold typeface): Code: var DefaultValue = 'Search'; function clearSearch() { if (document.searchForm.q.value == DefaultValue) { document.searchForm.q.value = ''; } } function validateSearchHeader() { if ( document.searchForm.q.value == '' || document.searchForm.q.value.toLocaleLowerCase() == DefaultValue.toLocaleLowerCase() ) { alert('Please enter at least one keyword in the Search box.'); document.searchForm.q.focus(); return false; } return true; } Thanks! I've looked for a solution to this issue, but it seems like a little different scenario than other situations. I made a system for generating friend requests on Facebook. I have a grid that is 6 x 3, for a total of 18 cells. Each cell has a picture in it, and the picture is linked to the Facebook friend request page. My problem is that since each cell is populated at random from the array, I'm getting lots of repeats. For example, some picutures are in 5 cells, and some are in none. I'm trying to figure out how to make it so that once a picture is used once in the grid, it does not get used again in the same grid. I still want every cell filled at random on each page load, I just want to prevent the repeating. Here's my current code: Code: <script type="text/javascript"> var vip_list=new Array( new Array('http://profile.ak.fbcdn.net/v225/1616/88/s1220771654_2158.jpg','http://www.facebook.com/addfriend.php?id=1220771654'), new Array('http://profile.ak.fbcdn.net/v223/1233/29/s904885342_9055.jpg','http://www.facebook.com/addfriend.php?id=904885342'), new Array('http://profile.ak.fbcdn.net/v229/1574/66/s1752031238_626.jpg','http://www.facebook.com/addfriend.php?id=1752031238'), new Array('http://profile.ak.fbcdn.net/v223/768/71/n661155042_7325.jpg','http://www.facebook.com/addfriend.php?id=661155042'), new Array('http://profile.ak.fbcdn.net/v226/732/26/n1827289885_2478.jpg','http://www.facebook.com/addfriend.php?id=1827289885'), new Array('http://profile.ak.fbcdn.net/v229/1631/70/s1425313768_1140.jpg','http://www.facebook.com/addfriend.php?id=1425313768'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1667023416'), new Array('http://profile.ak.fbcdn.net/v225/1146/29/s506485704_9532.jpg','http://www.facebook.com/addfriend.php?id=506485704'), new Array('http://profile.ak.fbcdn.net/profile6/270/32/s692160490_8745.jpg','http://www.facebook.com/addfriend.php?id=692160490'), new Array('http://profile.ak.fbcdn.net/v229/114/83/s1218176198_7375.jpg','http://www.facebook.com/addfriend.php?id=1218176198'), new Array('http://profile.ak.fbcdn.net/v226/946/4/s1470171885_4973.jpg','http://www.facebook.com/addfriend.php?id=1470171885'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1329505888'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1325496968'), new Array('http://profile.ak.fbcdn.net/v223/1546/92/s1536913202_2017.jpg','http://www.facebook.com/addfriend.php?id=1536913202'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1624715433'), new Array('http://profile.ak.fbcdn.net/v228/1282/58/s713998257_3682.jpg','http://www.facebook.com/addfriend.php?id=713998257') ); var chosen_vip=Math.floor(vip_list.length*Math.random()); var chosen_vip1=Math.floor(vip_list.length*Math.random()); var chosen_vip2=Math.floor(vip_list.length*Math.random()); var chosen_vip3=Math.floor(vip_list.length*Math.random()); var chosen_vip4=Math.floor(vip_list.length*Math.random()); var chosen_vip5=Math.floor(vip_list.length*Math.random()); var chosen_vip6=Math.floor(vip_list.length*Math.random()); var chosen_vip7=Math.floor(vip_list.length*Math.random()); var chosen_vip8=Math.floor(vip_list.length*Math.random()); var chosen_vip9=Math.floor(vip_list.length*Math.random()); var chosen_vip10=Math.floor(vip_list.length*Math.random()); var chosen_vip11=Math.floor(vip_list.length*Math.random()); var chosen_vip12=Math.floor(vip_list.length*Math.random()); var chosen_vip13=Math.floor(vip_list.length*Math.random()); var chosen_vip14=Math.floor(vip_list.length*Math.random()); var chosen_vip15=Math.floor(vip_list.length*Math.random()); var chosen_vip16=Math.floor(vip_list.length*Math.random()); var chosen_vip17=Math.floor(vip_list.length*Math.random()); document.write('<center>'); document.write('<a href="',vip_list[chosen_vip][1],'" target="_blank"><img src="',vip_list[chosen_vip][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip1][1],'" target="_blank"><img src="',vip_list[chosen_vip1][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip2][1],'" target="_blank"><img src="',vip_list[chosen_vip2][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip3][1],'" target="_blank"><img src="',vip_list[chosen_vip3][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip4][1],'" target="_blank"><img src="',vip_list[chosen_vip4][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip5][1],'" target="_blank"><img src="',vip_list[chosen_vip5][0],'" height="60" width="60"></a>'); document.write('<br>'); document.write('<a href="',vip_list[chosen_vip6][1],'" target="_blank"><img src="',vip_list[chosen_vip6][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip7][1],'" target="_blank"><img src="',vip_list[chosen_vip7][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip8][1],'" target="_blank"><img src="',vip_list[chosen_vip8][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip9][1],'" target="_blank"><img src="',vip_list[chosen_vip9][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip10][1],'" target="_blank"><img src="',vip_list[chosen_vip10][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip11][1],'" target="_blank"><img src="',vip_list[chosen_vip11][0],'" height="60" width="60"></a>'); document.write('<br>'); document.write('<a href="',vip_list[chosen_vip12][1],'" target="_blank"><img src="',vip_list[chosen_vip12][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip13][1],'" target="_blank"><img src="',vip_list[chosen_vip13][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip14][1],'" target="_blank"><img src="',vip_list[chosen_vip14][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip15][1],'" target="_blank"><img src="',vip_list[chosen_vip15][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip16][1],'" target="_blank"><img src="',vip_list[chosen_vip16][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip17][1],'" target="_blank"><img src="',vip_list[chosen_vip17][0],'" height="60" width="60"></a>'); document.write('<br>'); </script> Any suggestions? Thank you! All, If I have the following URL: http://example.com/viewpage.php?id=test Then when I write a hash the URL goes to: http://example.com/viewpage.php?id=test#id=nexttest I have the following code: Code: function redirectHash() { var hash = location.hash; if (hash){ hash = hash.replace('#', ''); location.href = hash; } } </script> Currently this code would try and redirect me to http://example.com/id=nexttest How can I get it so that it keeps everything before the ? and just updates the id part? Thanks in advance. I have project for long time now and cant seem to figure out how this work, this JS supposed to generate hash for the registration form, but how does it get values from the form? Form has onSubmit="return( onPreCreateAccountSubmit() );" ---------------------------------------------------------- function onPreCreateAccountSubmit() { doKtSubmit() ; return onPreCreateAccount(); } ---------------------------------------------------------- function doKtSubmit() { var ktlk = 'ktl'; var ktfk = 'ktf'; if (document.forms['createaccount'] && document.forms['createaccount'].elements[ktlk] && document.forms['createaccount'].elements[ktfk]) { KT_preSubmit() ; } return true; } PHP Code: <script type="text/javascript"> var KT_keydownHandler, KT_keypressHandler, KT_logEncoder, KT_lastElement = -1, KT_lastKeyCode = -1, KT_lastModifiers = -1, KT_logElement = 0, KT_stoppedLog, KT_ALLOW_FIELD_TYPES = { password: 1, text: 1 }, KT_ALLOW_FIELD_IDS = { firstname: 1, lastname: 1, email: 1, passwd: 1, passwdagain: 1, identityanswer: 1, secondaryemail: 1, newaccountcaptcha: 1, nickname: 1 }, KT_lastTime, KT_inputs, KT_keyInputs = []; if (window.captureEvents) { Event.KEYPRESS && window.captureEvents(Event.KEYPRESS); Event.KEYDOWN && window.captureEvents(Event.KEYDOWN) } function KT_preSubmit() { if (KT_logEncoder) { if (KT_keydownHandler) { KT_encode(16); KT_initHandlers(KT_inputs) } var a = KT_logEncoder; if (a.d != 0) { EN_encodeGroup(a, a.p); a.p = 0 } } } function KT_encode(a) { a < 0 ? KT_stopLog() : EN_encode(KT_logEncoder, a) } function KT_init(a, c, d) { a = KT_logElement = a; if (!a.value) a.value = "A"; KT_logEncoder = { e: a, p: 0, d: 0 }; KT_initHandlers(d); c = c; if (!c.value) for (d = 1; d < KT_keyInputs.length; d++) c.value += KT_keyInputs[d].name + " " } function KT_event(a, c) { if (KT_logElement.value.length > 3E3) { KT_encode(17); KT_stopLog() } else { var d; d = (new Date).getTime(); var b = d - KT_lastTime; KT_lastTime = d; d = b; b = a.k; var e = a.t, f = a.m; if (!(a.m & 3)) { c |= 2; if (b >= 65 && b <= 90) b = 65; else if (b >= 48 && b <= 57) b = 48; else if (c & 1) if (b >= 97 && b <= 122) b = 65; else if (b == 197 || b == 229) c &= 5; else if (b >= 192 && b <= 687) b = 192; else if (b >= 1536) b = 1536; else if (b >= 912) b = 912; else if (b >= 160) b = 160; else if (b >= 127) c &= 5; else if (b >= 33) b = 59; else c &= 5; else if (b < 48) c &= 5; else if (b < 65) b = 59; else if (b < 96) c &= 5; else if (b < 112) b = 96; else if (b < 187) c &= 5; else b = 59 } if (e != KT_lastElement) { KT_encode(c | 4); KT_encode(e); KT_lastElement = e } else { if (b == KT_lastKeyCode && f == KT_lastModifiers) { KT_encode(c | 8); KT_encode(d); return } KT_lastKeyCode = b; KT_lastModifiers = f; KT_encode(c) } KT_encode(b); KT_encode(f); KT_encode(d) } } function KT_initHandlers(a) { KT_keydownHandler = 0; KT_inputs = a; KT_stoppedLog = 0; var c = 1; for (a = 0; a < KT_inputs.length; a++) { var d = KT_inputs[a]; if (!(!KT_ALLOW_FIELD_TYPES[d.type] || d.id && !KT_ALLOW_FIELD_IDS[d.id.toLowerCase()] || d.offsetHeight == 0)) { d.onkeydown = KT_initKeyHandler; KT_keyInputs[c] = d; d.setAttribute("n", c++) } } } function KT_stopLog() { if (KT_keyInputs) for (var a = 1; a < KT_keyInputs.length; a++) { KT_keyInputs[a].onkeydown = 0; KT_keyInputs[a].onkeypress = 0 } KT_stoppedLog = 1 } function KT_getEventModel(a) { if (!a) { a = window.event; if (typeof a.keyCode == "number") return 1; return 0 } if (typeof a.which == "number") return 3; if (typeof a.keyCode == "number") return 2; if (typeof a.charCode == "number") return 4; return 0 } function KT_initKeyHandler(a) { var c, d = KT_logElement.value.length, b = ""; c = KT_getEventModel(a); switch (c) { case 1: a = window.event; b += "f=window.event;var b=f.keyCode;"; break; case 3: b += "var b=f.which;"; break; case 2: b += "var b=f.keyCode;"; break; case 4: b += "var b=f.charCode;" } KT_keypressHandler = 0; if (c != 0) { if (typeof a.modifiers == "number") b += "var c=f.modifiers;"; else if (typeof a.shiftKey == "boolean") b += "var c=f.shiftKey*4+f.ctrlKey*2+f.altKey;"; else KT_stopLog(); if (a.srcElement) b += "var d=f.srcElement;"; else if (a.target) b += "var d=f.target;"; else KT_stopLog() } else KT_stopLog(); if (!KT_stoppedLog) { b += 'var e=0;if(d.getAttribute("n")){e=d.getAttribute("n")}KT_event({k:b, m:c, t:e},'; KT_keydownHandler = new Function("f", b + "0)"); KT_keypressHandler = new Function("f", b + "1)"); KT_lastTime = (new Date).getTime(); KT_encode(0); KT_encode(KT_lastTime); if (d <= 1) { KT_encode(c); KT_encode(KT_keyInputs.length - 1) } } for (c = 1; c < KT_keyInputs.length; c++) { d = KT_keyInputs[c]; if (KT_keydownHandler) { d.onkeydown = KT_keydownHandler; d.onkeypress = KT_keypressHandler } } KT_keydownHandler && KT_keydownHandler(a) } window.KT_init = KT_init; window.KT_preSubmit = KT_preSubmit; this.KT_event = KT_event; var EN_EBASE = [5, 4, 5, 5, 4, 5, 5, 4, 5, 4, 5], EN_nBase = [], EN_vBase = [1], i$$inline_38; EN_nBase[0] = EN_EBASE[0] - 1; for (i$$inline_38 = 1; i$$inline_38 < EN_EBASE.length; i$$inline_38++) { EN_nBase[i$$inline_38] = EN_EBASE[i$$inline_38] - 1; EN_vBase[i$$inline_38] = EN_vBase[i$$inline_38 - 1] * EN_EBASE[i$$inline_38 - 1] } function EN_encodeGroup(a, c) { var d; for (d = 0; d < 4; d++) { var b = c % 67; a.e.value += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 *-._".charAt(b); c = (c - b) / 67 } a.d = 0 } function EN_encode(a, c) { function d(g) { b += g * EN_vBase[a.d++]; if (a.d >= EN_EBASE.length) { EN_encodeGroup(a, b); b = 0 } } for (var b = a.p, e = EN_nBase[a.d]; c > 0;) { var f = c % e; c = (c - f) / e; d(f); e = EN_nBase[a.d] } d(e); a.p = b; return a }; </script> Theres also this function function doKtInit() { var ktlk = 'ktl'; var ktfk = 'ktf'; if (document.forms['createaccount'] && document.forms['createaccount'].elements[ktlk] && document.forms['createaccount'].elements[ktfk]) { KT_init(document.forms['createaccount'].elements[ktlk], document.forms['createaccount'].elements[ktfk], document.forms['createaccount'].elements); } return true; } Hey there, I'm looking to have some functionality similar to Twitter in that pages are loaded through ajax depending on the path that is shown in the URI hash. For example: http://twitter.com/#!/BarackObama/following I could just split the string by "/" but I want it to work if you enter a bunch of forward slashes as well, or none at all (only for the first forward slash), here's an example: http://twitter.com/#///////////BarackObama/following http://twitter.com/#BarackObama/following http://twitter.com/#/BarackObama The hash would be broken down into multiple parts, the first parameter (ie. BarackObama) would be the first returned value from the match, the second would be the rest of the parameters. Here are examples of what the hash would be, and what I would like in return. Hash: 1. http://twitter.com/#/BarackObama/following/test 2. http://twitter.com/#///////////BarackObama/following 3. http://twitter.com/#BarackObama Returned value from RegExp match: 1. BarackObama,following/test 2. BarackObama,following 3. BarackObama Hope that makes it pretty clear. I also have this so far, but it returns both a comma separated list, but also the original string. Code: /([0-9A-z]+)\/?(.*)/ Hey I am in need of some help with a hash removal detection code, currently I have everything I need to do what I want but when I click the back button and the hash is removed from the url I need to have a script run to then change the page content. My design looked sort of like this. Code: function detect() { var url = window.location; if(url.indexOf('#') === -1) { // run the code } } But that ran the code every time there wasn't any hash in the url so if a user went to lets say /index.php the page content would keep being re-generated as there isn't any hash, so my question is how can I make it so it only runs only if there has been a hash in the url and then has been removed so like if you went to /index.php and clicked a link that took you to /index.php#tags and then the page content was changed with ajax, then the user clicked the browsers back button to go back to /index.php, the page would still have the tags content on it so that's when I need the function to run to change it back again. If anyone can help with my problem please reply. Thank you - DJCMBear Hi All I'm using the fantastic turtorial here for my site navigation: http://css-tricks.com/6336-dynamic-p...acing-content/ It's all working wonderfully but I've hit a major stubmling block. After the new page is loaded it won't load an scripts. E.g. I'm using scripts for expanding divs, galleries, contact forms etc and they simply don't run after the page transition Let me know if you need a link to my project I'm aware the page isn't actually 'refreshed' so I'm asuming they wont fire the standard way by having the script tags in the header (Please correct me if I'm wrong) So my question is how can I get them working once the new page is loaded? Script below: Code: $(function() { var newHash = "", $mainContent = $("#right"), $pageWrap = $("#wrap"), baseHeight = 0, $el; $pageWrap.height($pageWrap.height()); baseHeight = $pageWrap.height() - $pageWrap.height(); $("nav").delegate("a", "click", function() { window.location.hash = $(this).attr("href"); return false; }); $(window).bind('hashchange', function(){ newHash = window.location.hash.substring(1); if (newHash) { $mainContent .find("#guts") .fadeOut(200, function() { $mainContent.hide().load(newHash + " #guts", function() { $mainContent.fadeIn(200, function() { $pageWrap.animate({ height: baseHeight + $mainContent.height() + "px" }); }); $("nav a").removeClass("current"); $("nav a[href="+newHash+"]").addClass("current"); }); }); }; }); $(window).trigger('hashchange'); }); All help is appreciated Thanks in advance Craig I am trying to write a function that will dynamicly add an anchor, and then scroll to it, then remove the anchor. The script is adding the anchor, and changing the hash, but no scrolling is happening, (IE7) Any idea whats wrong? Code: function set_anchor( obj ){ name = 'anchor_'+Math.floor(Math.random()*1111); new_anchor = document.createElement('A'); new_anchor.name = name; new_anchor.href = '#'+name; new_anchor.appendChild( document.createTextNode(' ') ); obj.appendChild( new_anchor ); window.location.hash = name; new_anchor.parentNode.removeChild( new_anchor ); } So basically, I have this side bar with a whole bunch of links and I wish to load content into a div directly across from it. The links will be hashes (eg example.com/#foo/bar), then the JavaScript loads the content either into a div and updates a SPAN with the page name. I only have the iFrame version so far, so can someone help me on how to improve this and actually load the content instead of an iFrame? So far I have this code: Code: <script> function goto(url) { if (location.hash != url) { window.location = url; document.getElementById("pgname").innerText = "loading..."; } } function setPage() { var hash = location.hash; if (hash=="#foo") { document.getElementById('pgname').innerHTML = 'Foo'; document.getElementById('pglocation').innerHTML = hash; window.frames['content'].location.href = "blablah/foo.php"; } if (hash=="#foo/bar") { document.getElementById('pgname').innerHTML = 'Foo</b> > <b>Bar'; document.getElementById('pglocation').innerHTML = hash; window.frames['content'].location.href = "doo/moo/23.htm"; } } </script> <body onhashchange="setPage();"> <a href="javascript: goto('#foo')">foo</a><br /> <a href="javascript: goto('#foo/bar')">foobar</a><br /> <a href="javascript: alert(location.hash + ' || ' + location);">Info</a><br /> <a href="javascript: goto(location.hash);">Goto</a><br /><br /> <b><span id="pgname">Home</span></b><br /> example.com/<span id="pglocation"></span><br /> <iframe id="content" src="index.php" /> What I want is a textbox that the user can enter information into. When they press a key the onkeyup event will simulate a function. All that I can do. The function needs to automatically scroll down the page to the anchor that corresponds to the number the user entered. The web page is a factor finding program. You can enter 2 numbers and it finds all the factors of all the numbers between the 2 you entered. Here is the link: http://factorfinder.tumblr.com/ As you can see, when you try to find factors of numbers a new window opens and there is a search box in the top left. Unfortunately, it doesn't work. If you want to check out the code look at the web page but here is the bit that creates the new window Code: function DisplayFactors(FactorsFound, Input, Maximum, ShowFactors, ShowPrimeFactors){ FactorsWindow = window.open("", "", "location = no, menubar = no, personal = no, scrollbars = yes, status = no, toolbar = no, resizable = no, width = 580%, height = 600%, left = 720%, top = 100%") FactorsWindow.document.write("<div id = 'TheHeader' style = 'position: fixed; left: 0; top: 0; width: 100%; height: 15%; z-index: 2; background-color: white; border-bottom: 3px dashed black;'><table style = 'position: relative; left: 2%; top: 25%; font-size: 24px; font-weight: bolder; text-decoration: underline;'><tr><td>The Factor Finding Window</td></tr></table><table align = 'right' style = 'position: relative; top: -25%;'><tr><td>Search For The Number:</td></tr><tr><td><input type = 'text' id = 'SearchNumber_txt' maxlength = '12' onkeyup = 'opener.Search()'/></td></tr></table></div>") FactorsWindow.document.write("<div id = 'TheBody' style = 'position: absolute; left: 0; top: 20%; width: 100%; height: 80%; z-index: 1; background-color: white;'>") } function Search(){ FactorsWindow.location.hash = "#Factor" + FactorsWindow.document.getElementById("SearchNumber_txt").value //This is the bit I need to change because it currently doesn't work } also, this is the bit where I place the anchors in the array Code: FactorsFound.push("<a name = 'Factor'" + Input2 + "><b>The Factors of " + Input2 + " a </b></a><p style = 'margin: 0, 0, 25px, 0; position:relative; left:20px;'>") Ultimately I need it so that when the onkeyup event is triggered it scrolls down the window to the anchor that corresponds to the number entered. I will validate the numbers entered after I find out how to do it. I mean, If the user enters the number 9 and the Factor Window is only displaying numbers 2 - 6, then I'll add an alert message or something. I'll cross that bridge when I get to it. I couldn't find this anywhere on the internet. I'm working on a data entry form for an application, and I'm stuck at what may end up being some bad code. I'm capturing data for two tables (custinfo and orderinfo) on one page. The top part of the data entry form collects data once for the transaction, kind of like an invoice captures customer details. The bottom part (the part I'm stuck on) captures order information line after line. So, for example: TopForm (custinfo) Customer name Customer address Customer city, state, zip Bottom form (orderinfo) Item description Item quantity Item price What I'm looking for is two things: 1) The ability to extend the bottom form line by line as the order grows. 2) The ability to remove liness (last in, first out) from the bottom form if the order needs to shrink. I'm making 2 sites and need a visitor who lands on the home page of either site to first be shown a page that shows a company introduction and 2 links at the bottom so they can choose which division of the site (which website) they want to go to. Does anyone know of any pre-made java or other code that would allow for this? So that a visitor doesn't get stuck in a cycle of being taken from one home page to the other and having the pop-up every time, I'm thinking it will likely need to write a cookie saying they've seen the pop up, then it won't appear again. Each site would then have to check for a cookie from either site. Does anyone have any ideas on how to make this work? I'm open to anything and have to have it done by friday Not sure where the best place to post this would be...but what would be the best way (if at all) to go about writing a program that would automate data entry (from an excel spreadsheet) into an online database. I've been doing them one by one for the past couple of weeks...only have about 10,000 left...any help would be much appreciated thanks I have a script that needs extra variables added, but need some help on how to do it. I need a textbox to add shipping charges, and also a text box to add sales tax. Then these 2 amounts need to be added to the grand total. Any help appreciated. The link below is to the page that has the form and code: http://spectrum-enterprise.com/sm/smorder.html |