Jump to content

button which links to random post on site

Recommended Posts

Posted

I'm trying to get a button on my home page that links to a random post on my site, but have no idea how to do it. I've tried to look at a couple other answers... can anyone out there help simplify it for me?

  • Replies 5
  • Views 1.3k
  • Created
  • Last Reply

Top Posters In This Topic

Posted

<script>

  $( ( ) => {
  
    /*
    
      begin change button url to random page
      
      Version       : 0.1d0
      
      SS Version    : 7.1
      
      Dependancies  : jQuery
      
      By            : Thomas Creedon < http://www.tomsWeb.consulting/ >
      
      */
      
    // enter button block id without the #
    
    const buttonBlockId = '#block-yui_3_17_2_1_1706584972305_1843 a';
    
    const urlPrefix = '/read-this-trashh';
    
    // do not change anything below, there be the borg here
    
    let r = Math.floor ( Math.random ( ) * 100 );
    
    r = String ( r ).padStart ( 3, '0' );
    
    const url = urlPrefix + r;
    
    $( '#' + buttonBlockId + ' a' ).attr ( 'href', url );
    
    } );
    
    /* end change button url to random page */
    
  </script>

Posted

when it worked that code seemed to only drop me at the category page, not a random page within the category (if it was the code that was doing that)

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.