How to BLEND a randomizing function to SHUFFLE several links before OPEN all of them on different tabs from ONE single click with javascript or jQuery

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.

Answer

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>