July 12, 201313 yr Hi Guys I need some help, I want to do the following: Have a button that when clicked will go through to a random website, the websites will all be in a JavaScript array. The website that is chosen from the button click needs to be completely random and not the same if the user visits the site again. Does anyone know an easy way to do this using JavaScript? I thought of just using an Array and go to the next one after each click but that is not random. Is there a way that we can have a constant loop through the array of websites and when the user clicks it stops on a website? Let me know guys I know i'm asking a lot so any input you can give would be greatly appreciated. Thanks Luke
July 12, 201313 yr <script type="text/javascript"> //var nextWord = (function() { // var wordArray = ['fe','fi','fo','fum']; // var count = -1; // return function() { // return wordArray[++count % wordArray.length]; // } //}()); </script> <script type="text/javascript"> var nextWord = (function() { var wordArray = ['fe','fi','fo','fum']; var copy; return function() { if (!copy || !copy.length) copy = wordArray.slice(); return copy.splice(Math.random() * copy.length | 0, 1); } }()); </script> <p id="foo"> </p> <button onclick="document.getElementById('foo').innerHTML = nextWord();">Update</button> You can find this solution here: http://stackoverflow.com/questions/7843467/continuously-loop-through-javascript-text-array-onclick Hope this helps.
July 12, 201313 yr http://jsfiddle.net/CgbMK/6/ Could be improved heavily, just threw it together though
July 12, 201313 yr http://jsfiddle.net/CgbMK/6/ Could be improved heavily, just threw it together though yes this one much better
July 12, 201313 yr or you could use this <button id="random">random</button> var links = new Array(); links[0] = "link 1"; links[1] = "link 2"; links[2] = "link 3"; links[3] = "link 4"; links[4] = "link 5"; links[5] = "link 6"; $('#random').bind('click', function () { // Get ammount of links in the array var urls = links.length; // Get a random ID to show var show = Math.floor((Math.random()*urls)); // Show the url or what ever you want to do alert(links[show]); //will open the link in a new window //window.open(links[show],'_blank'); // Remove the link from the array delete links[show]; // Reorder array links = $.grep(links,function(n){ return(n) }); console.log(links); }); Edited July 12, 201313 yr by D4Y0
July 13, 201313 yr Author Hey guys Thanks so much for your help with this, I have tried all of them though and can not get any of them to work any ideas why?
July 13, 201313 yr my one you need to include the jQuery libary like so: <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
July 14, 201313 yr Are you sure "shuffle" won't be better than "random"? I mean, random doesn't gurantee that the previously visited website won't be selected... In case of shuffle, say you have 5 websites and the user clicks the button; a random website gets opened in a new tab and when the user clicks the button again the next website will be selected from the remaining 4. If you want to leave the website you might wanna remove the visited websites from the list by saving the list of visited websites in a cookie. This might help you start: https://github.com/danjordan/jquery.shuffle Edited July 14, 201313 yr by Valentin
Create an account or sign in to comment