Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 12:06 PM   PM User | #1
snarkeybarkey
New to the CF scene

 
Join Date: Jul 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
snarkeybarkey is an unknown quantity at this point
Article of the Week Coding

Hello, I've done a lot of google searching looking for something like this but haven't found it yet. The idea is that i want to have on our website an article of the week, basically an article randomly selected from all of the ones we have that will display on the index page for a week's time, as a way of promoting older stuff that might not otherwise be seen.

The closest I found was a script for a link of the day, but the link for each day is not randomly chosen.

Has anyone seen a code for something like that? Basically I'd enter into the code every link from which to be chosen, and at the beginning of every week it would randomly choose one to display for that entire week.

Thanks for any input or help.
snarkeybarkey is offline   Reply With Quote
Old 11-20-2012, 01:08 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Why do you want it to be random? In that case you will need to use a cookie to store the random article in the user's computer so that the same article will be displayed throughout the whole week.

It would be simpler (and perhaps more effective) just to display a random article each time the user visited your site.

If you want every user to see the same article then there is no random element involved.

This code will display a quotation based on the week number. You can easily adapt it for your requirements. Of course, while you must have 53 "quotations" (links) these can be duplicated, and arranged so as to appear to be random.


Code:
<div id = "quoteOfWeek"></div>

<script type = "text/javascript">
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1); // month 0 is January
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
} 

var today = new Date();
var weekno = today.getWeek(); 

var Quotation = [];
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";  // note escape quotes
Quotation[9] = "Invention is the mother of too many useless toys.";
Quotation[10] = "Time is of the essence! Comb your hair.";

// and so on for 53 weeks

document.getElementById("quoteOfWeek").innerHTML=Quotation[weekno];

</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-20-2012 at 01:16 PM..
Philip M is offline   Reply With Quote
Old 11-20-2012, 01:50 PM   PM User | #3
snarkeybarkey
New to the CF scene

 
Join Date: Jul 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
snarkeybarkey is an unknown quantity at this point
The idea is for the same article to be shown to all users for the entire week, but for the article to be chosen at random. If there wasn't an easy solution, we'd just pick one article to put a link to each week...

And I realize now, looking at your code, the folly of what I'm asking for - the code itself could have no way of remembering which one it randomly picked for the duration of the week. I'd essentially have to have a code that referenced another file on the server that specified which one was picked at random. And that sounds way too crazy for someone who can't code, nor would want to pay so much for someone to make something so trivial to begin with.

Thanks for your time.
snarkeybarkey is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:21 AM.


Advertisement
Log in to turn off these ads.