Genuinely Random Link Generator

By VenryxVenryx

Description

Up until now, there were two ways to create random links:

  • Display all of the pages in a mini-block, filled with tiny, black rectangles which link to each of the pages. Because they're so small and lack identification, the person ends up navigating to an essentially random page.
  • Have a link to the page 'random', where you add the ListPages module to sort the pages and redirect to the first of the list. This is genuinely random, but the 'randomly-sorted list' is only re-shuffled every minute, meaning that you can't keep clicking it to see more randomly.

Based on the second method, I've figured out a way to combine ListPages and Javascript to provide genuine randomness without having to wait for the ListPages module to re-shuffle.

There are two versions of this method. The first is easy to implement, but requires two page redirections. The second is harder to implement and involves the use of i-frames, but cuts the process down to only one redirection.

Instructions

Simple Version (2 page redirects)

1. Create a link in the Sidebar to the page 'nav:random'.

2. Follow the link, and create the page 'nav:random' with this code:

[[html]]
<html>
<body>
    <script type="text/javascript">
        window.parent.location.replace(document.URL.substring(0, document.URL.indexOf("/local")) + "/nav:random-redirect/offset/" + Math.random());
    </script>
</body>
</html>
[[/html]]

Note: After saving the page above, you'll be redirected to 'nav:random-redirect'.

3. After being redirected, create the page 'nav:random-redirect' with this code:

[[module ListPages category="_default" order="random" limit="1" offset="@URL|0"]]
[[iframe https://files.snippets.obscurative.ru/local--code/code:iframe-redirect#%%link%% frameborder="0" width="1" height="1"]]
[[/module]]

Now you've got a random-page link on your sidebar that can clicked as frequently as you like without caching its results.

(Note: If you need to edit or delete the pages, add '/norender/true' to the end of the page address.)

Complex Version (1 page redirect)

1. In the Sidebar, add the line below: (replacing "<WEBSITE ADDRESS>" with the address of your website, e.g. "http://venryx.wikidot.com", and replacing "17px" with an appropriate height for the link we're about to put on your sidebar.)

[[iframe <WEBSITE ADDRESS>/local--code/nav:random transparency="true" frameborder="0" height="17px"]]

2. Create the page 'nav:random' with this code: (replacing the CSS between the "<style>" tags with whatever gets the link to look like the others on your website's sidebar. (you may need to "Save & Continue" the page and view the sidebar on another tab to get the link to look right, and you may need to look through your website's CSS file as well.))

[[module CSS]]
.code {border: 0; background-color: transparent; display: none;}
[[/module]]
[[code type="html"]]
    <style type="text/css" id="internal-style">
        @import url(https://d3g0gp89917ko0.cloudfront.net/v--c4924039f145/common--theme/base/css/style.css);
        a {color: #444;text-decoration: none;}
        a:hover {color: #76ae45;text-decoration: none;background-color: transparent;}
        body {font: 13px/1.3em "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;}
    </style>
    <script type="text/javascript">
    if (window.location == window.parent.location)
        clicked();
    function clicked()
    {
        window.parent.location.replace(document.URL.substring(0, document.URL.indexOf("/local")) + "/nav:random/offset/" + Math.random());
    }
    </script>
    <a href="" onclick="clicked()">Random</a>
[[/code]]
[[module ListPages category="_default" order="random" limit="1" offset="@URL|0"]]
[[iframe https://files.snippets.obscurative.ru/local--code/code:iframe-redirect#%%link%% frameborder="0" width="1" height="1"]]
[[/module]]

Now you've got a random-page link on your sidebar that can clicked as frequently as you like without caching its results.

(Note: If you need to edit or delete the pages, add '/norender/true' to the end of the page address.)

In action

The complex version: http://gustrength.com


Thanks to tsangk for this great snippet: conditional-blocks


text above inserted with:

[[include :snippets:if START |unique=1|type=equal|var1=%%name%%|var2=conditional-blocks]]
**##red|Thanks to tsangk for this great snippet:##** [[[code:conditional-blocks]]]
[[include :snippets:if END]]



Other snippets posted by Venryx


Rate this solution

If you think this solution is useful — rate it up!

rating: +5+x
Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License