1

I need to BLEND this function with a function to RANDOMIZE my list before OPEN all my links, all done from a single click.

This question is not about to Randomize, nor how to Open several links. It is about to BLEND both functions to be performed from one single click.

I need a javascript able to open all my links, each of them on its own tab, in different random order each time, from one single click.

I know how to randomize elements, and how to open several links each of them on its own tab. However I can't figure out how BLEND them together to randomize several links before opening all of them on its own tab from one single click.

I have searched a lot and found a few similar posts but none of them got its question solved, as their replies teach how to randomize all links to open only one of the links which is NOT what I'm asking here.

Again, I'm looking for one javascript able to open all my links, each of them on its own tab, in different random order each time, from one single click.

This is my current code to open several links all of them on its on tab, but of course, without finding a way to blend a randomizing function with it, all of my links still open in the same order, obviously:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div><span class="randomLinks">Random Links</span></div>

<script>
$('.randomLinks').click(function(randomLinks) {
    randomLinks.preventDefault();
    window.open('https://link-01.moc');
    window.open('https://link-02.moc');
    window.open('https://link-03.moc');
    window.open('https://link-04.moc');
    window.open('https://link-05.moc');
    window.open('https://link-06.moc');
    window.open('https://link-07.moc');
    window.open('https://link-08.moc');
    window.open('https://link-09.moc');
    window.open('https://link-10.moc');

});
</script>

I'm open to use any suggestion to solve this, as I know there are dozens of algorithms and functions to shuffle elements, but I can't find any way to blend them with this code.

I don't mind to change completely my code, as long as it opens all my links each of them on a different tab, in a different random order each time.

rx65m
  • 105
  • 6
  • Put the link strings in an array and then randomly pick an index and open that link, and track what indices have previously been chosen so you don't select the same index twice (unless that doesn't matter) – devlin carnate Jun 23 '21 at 19:27
  • @devlincarnate Thank you, can you be so nice to explain how I do that? I really don't understand.. – rx65m Jun 23 '21 at 19:29
  • 1
    Does this answer your question? [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – devlin carnate Jun 23 '21 at 19:30
  • @devlincarnate No. I tried that and I could not blend it with the code I showed here. – rx65m Jun 23 '21 at 19:32

2 Answers2

1

You need one Array of object that keep url and random order and sort the array of object by order and finally open the all of urls.

Here is sample code:

<button id="randomLinks" type="button">Open liks</button>

<script>

    var objects = [
        { url: "https://link-01.mochttps://link-01.moc" },
        { url: "https://link-02.mochttps://link-02.moc" },
        { url: "https://link-03.mochttps://link-03.moc" },
        { url: "https://link-04.mochttps://link-04.moc" },
        { url: "https://link-05.mochttps://link-05.moc" },
        { url: "https://link-06.mochttps://link-06.moc" },
        { url: "https://link-07.mochttps://link-07.moc" },
        { url: "https://link-08.mochttps://link-08.moc" },
        { url: "https://link-09.mochttps://link-09.moc" },
        { url: "https://link-10.mochttps://link-10.moc" },

    ];

    $('#randomLinks').click(function () {
        for (var i = 0; i < objects.length; i++) {
            var order = Math.floor(Math.random() * 1000) + 1 // random number
            objects[i].order = order;
        }


        objects.sort(function (a, b) {
            return (a.order - b.order);
        })

        for (var i = 0; i < objects.length; i++) {
            window.open(objects[i].url);
        }

    });
</script>
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
  • Alireza Ahmadi Thank you very much! This works perfectly fine! I appreciate a lot your kindness! – rx65m Jun 23 '21 at 20:24
  • 1
    Much appreciated – Alireza Ahmadi Jun 23 '21 at 20:29
  • `Math.random() * 1000` is fairly random, but you could still run into repeats, which would then drop links. Also this adds an unnecessary loop to create a random order, you can just sort an array with a compare function that returns a random value. – PoorlyWrittenCode Jun 24 '21 at 02:30
  • Hello @alireza-ahmadi . I hope you can help with a similar problem I posted here: [link](https://stackoverflow.com/questions/68671282/how-to-build-links-with-fragments-taken-from-text-fields-but-within-a-javascrip) Thank you in advance. – rx65m Aug 05 '21 at 18:28
0

Hypothetically, you could just create an array of links and then sort that randomly.

But I can't read open a bunch of random links and not think that's a dick move.

let links = [
  'https://link-01.moc',
  'https://link-02.moc',
  'https://link-03.moc',
  'https://link-04.moc', 
  'https://link-05.moc',
  'https://link-06.moc',
  'https://link-07.moc',
  'https://link-08.moc'
];

//replace console.log with window.open
links.sort(() => Math.round(Math.random() * 2)).forEach(link => console.log(link));
PoorlyWrittenCode
  • 1,003
  • 1
  • 7
  • 10