3 Replies - 480 Views - Last Post: 30 January 2013 - 01:03 PM

#1 SevenKader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-January 13

How to get the "R" button to send users to a random post

Posted 30 January 2013 - 12:05 PM

How to get the "R" button to send users to a random post.
Like this website. http://theawesomer.com/
Is This A Good Question/Topic? 0
  • +

Replies To: How to get the "R" button to send users to a random post

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3227
  • View blog
  • Posts: 10,837
  • Joined: 12-December 12

Re: How to get the "R" button to send users to a random post

Posted 30 January 2013 - 12:51 PM

It uses the documents' keydown event:

	document.onkeydown = function(e){
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			 if(keycode == 39){ // back arrow
			 		document.onkeydown = "";
					location.href=nextpage;
			} else if(keycode == 37){ // forward arrow
					document.onkeydown = "";
					location.href=prevpage;
			} else if(keycode == 82){ // down arrow = random
					document.onkeydown = "";
					location.href="/?random="+Math.floor((Math.random()*100000)+1);
			}
		}

View the source for the page and you will see this code.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: How to get the "R" button to send users to a random post

Posted 30 January 2013 - 12:51 PM

Here is the magic (taken from the page you referenced and I commented)...

// Listen for a key being pressed down and call this function
// giving it the event keycode (the key value pressed)
document.onkeydown = function(e){
    if (e == null) { // ie
        keycode = event.keyCode;
    } else { // mozilla
        keycode = e.which;
    }

    // If that code was 82 (the value for the "R" key)
    // Set the window location to a new URL based off a random number
    if(keycode == 82){ // down arrow = random
        document.onkeydown = "";
        location.href="/?random="+Math.floor((Math.random()*100000)+1);
    }
}



This is what the site uses to detect the R key and change the URL to another page. Just look at the source of the page to see it there.

Hope that helps clarify things. :)

This post has been edited by Martyr2: 30 January 2013 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3227
  • View blog
  • Posts: 10,837
  • Joined: 12-December 12

Re: How to get the "R" button to send users to a random post

Posted 30 January 2013 - 01:03 PM

It redirects to the same page with a random number attached to the url. I believe there is some server-side code that intercepts this request and serves a page with different content and images.

If you want to re-direct to a random image on the current page then you could:

Give the images on the page the IDs 'image1', 'image2', etc., to 10 (for example)
Generate a random number between 1..10, following the example above
Use:

location.href = "currentpage.html#image" + yourRandomNumber;

In fact, you don't need the current page:
location.href = "#image" + yourRandomNumber;

This post has been edited by andrewsw: 30 January 2013 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1