How to get the "R" button to send users to a random post.
Like this website. http://theawesomer.com/
3 Replies - 339 Views - Last Post: 30 January 2013 - 01:03 PM
#1
How to get the "R" button to send users to a random post
Posted 30 January 2013 - 12:05 PM
Replies To: How to get the "R" button to send users to a random post
#2
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:
View the source for the page and you will see this code.
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.
#3
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)...
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.
// 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
#4
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:
In fact, you don't need the current page:
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
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote




|