Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with JavaScript

Featured Replies

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

<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.

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 by D4Y0

  • 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?

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 by Valentin

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.