Results 1 to 1 of 1

Thread: Picking a Random html page from an Array but each page only once

  1. #1
    Join Date
    Jul 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Picking a Random html page from an Array but each page only once

    I am creating a Html survey with multiple pages. (INDIVIDUAL HTML PAGES LIKE page1.html, page2.html etc.)

    I want to have the first and last two pages in the same order for all users.

    But I want to have middle pages random.

    Here is my code which works and creates random pages however, it does not load pages and show them as text.

    Code:
    let randomlinks = [ href='Task1A.html', href= 'Task1B.html', href = 'Task1C.html', href ='Task1D.html',
      href = 'Task2A.html', href = 'Task2B.html', href = 'Task2C.html', href = 'Task2D.html',
    href= 'Task3A.html', href= 'Task3B.html', href= 'Task3C.html', href = 'Task3D.html',
      href = 'Task4A.html', href = 'Task4B.html', href = 'Task4C.html', href = 'Task4D.html']
    
     let getRandomPage = function* (array) {
        let clone = [...array]
        let  getIndex =  function(arr){ return Math.floor(Math.random() * arr.length)};
        while(clone.length !== 0) yield clone.splice(getIndex(clone),1).pop();
        return window.location = 'Final.html'
     };
    
     let randoms = getRandomPage(randomlinks);
    
      const logRandom = () => {let val = randoms.next().value; if (val !== undefined) document.querySelector('html').textContent = val;};
    
    
     const load = () => {
     document.getElementById('random').addEventListener('click', logRandom);
     }
     document.addEventListener('DOMContentLoaded', load)
    
    <button type="submit" id="random" class="btn"  >Next </button>
    --------

    With this code, pages upload but they repeat in an infinite loop.

    Code:
    let randomlinks = [ href='Task1A.html', href= 'Task1B.html', href = 'Task1C.html', href ='Task1D.html',
      href = 'Task2A.html', href = 'Task2B.html', href = 'Task2C.html', href = 'Task2D.html',
    href= 'Task3A.html', href= 'Task3B.html', href= 'Task3C.html', href = 'Task3D.html',
      href = 'Task4A.html', href = 'Task4B.html', href = 'Task4C.html', href = 'Task4D.html']
    
    function randomlink(){
      window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
    
    
      }

    I want to stress that each page is separate I do not have a single HTML file. I HAVE multiple HTML files that I want to load randomly each time I press next button.

    Anyone can help me with this? Thank you.
    Last edited by james438; 07-16-2021 at 05:01 PM. Reason: format

Similar Threads

  1. Refresh a HTML page from another page
    By jay66online in forum HTML
    Replies: 5
    Last Post: 01-21-2009, 04:52 AM
  2. Loading first image in array into a div on same page
    By sany in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 07-09-2008, 03:54 PM
  3. script works in html page but not in php page Cross Browser marquee II ?
    By leonidassavvides in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 06-17-2007, 12:17 PM
  4. Replies: 5
    Last Post: 04-02-2007, 12:37 PM
  5. Random Headers on a HTML Page
    By tomyknoker in forum JavaScript
    Replies: 2
    Last Post: 05-26-2006, 07:47 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •