Web Analytics Made Easy -
StatCounter A button that randomly chooses urls from the list - CodingForum

Announcement

Collapse
No announcement yet.

A button that randomly chooses urls from the list

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • A button that randomly chooses urls from the list

    Good morning I am Anna. I am an artist and have a website with my drawings and would like to make a button that can randomly open a new page after it is pressed.

    As an example: Bored? Press the Bored Button!

    I tried to see the website source to find out the code that randomly chooses the pages but all I could find is the form html:

    <form name="bored" method="post" action="/random" class="button">
    <input type="image" name="submit" src="/i/boredButton.png" value="Bored No More" />
    <input type="hidden" value="submit" name="ie7sucks">
    </form>

    If you know what is the rest script code I need to add to my root directory where I can add the random pages I would be very thankful for your help.

  • #2
    Obviously this page determines the page server side. However you can use a simple solution by Javascript:
    Code:
            <button id="mybutton">Klick!</button>
            <script>
                var urls = [
                    "http://google.com",
                    "http://bing.com",
                    "http://maps.google.com"
                ];
                document.getElementById("mybutton").addEventListener("click", function() {
                    var idx = Math.floor(Math.random() * urls.length);
                    window.location.href = urls[idx];
                });
            </script>

    Comment


    • #3
      The rest of the script is called by the action="/random"

      You just need to figure out the site redirects to find the actual file containing the script.
      Stephen
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

      Comment


      • #4
        Originally posted by felgall View Post
        The rest of the script is called by the action="/random"

        You just need to figure out the site redirects to find the actual file containing the script.
        All the urls will be from my website for example:
        page1.html
        page2.html
        page3.html
        page4.html
        page5.html etc...

        I will add the html code of my pages:
        <button id="mybutton">Klick!</button>

        And what type of CSS and JS code I need to create? And where to put them? In the web.confiig file?

        Comment


        • #5
          And what type of CSS and JS code I need to create? And where to put them? In the web.confiig file?
          Didn't you read my posting? Just put the code into your HtML where you want the button to appear and adjust the URLs. That's all.

          Comment


          • #6
            And what about those without JS? They get a button that doesn’t do anything.
            If you’re doing it with JS alone then do it properly: Create and append the button itself with JS. This way people without JS won’t get the random page functionality but that’s still better than getting a non-functional button.

            On the other hand, you could do it with a sever-side script, as that other site is likely doing, which is a much more robust way to do this. I honestly don’t see a reason to suggest JS at all here.
            Stop solving problems you don’t yet have!

            Comment

            Working...
            X