Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Display an hyperlinked image daily - Help!

  1. #11
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Most grateful for your solution(s)

    John, you're a genius, I cant thank you enough and I also must thank 'Keyboard 1333' for responding to my post in the first place. I did have this query elsewhere on other forums but nobody responded so I am really grateful.

    John, the array which you mentioned at the end of your last post was what I was after all along and still would like to obtain in the future, that way I can use the links from external sites to populate the array and it would take the surfer there instantly for the indepth information about the subject. However, I can, and will still work with the other method of having the html pages folder on the server and having them link to those. It means more work for me creating the pages but it's not a big problem - indeed it may well be more beneficial in the long term because it would mean they don't leave the site - and anything to keep people on the site for a little bit longer is all good.

    This thread and your solutions has given me so many ideas for different dynamic content on my site and I'm eternally in your debt so thank you once again.

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    As for the array, you could use one for links and one for texts, they would be similar to what the script you originally posted did with images. Or use just one array for both (scroll the block, there are other changes below for this in the href and innerHTML lines. I also moved the declaration of the default image up into the configuration area.):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Daily Image - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <img id="dailyimage" src="images/default.gif" alt="daily image" title=""><br>
    <a id="dailylink" href="http://en.wikipedia.org/wiki/January_1" target="_blank">Wiki Dates</a>
    <script type="text/javascript">
    
    // Daily Image Script (c)2012 John Davenport Scheuer
    // as first seen in http://www.dynamicdrive.com/forums/
    // username: jscheuer1 - This Notice Must Remain for Legal Use
    
    ;(function(){
    	var folder = 'images/'; // set image folder
    	var diag = true; // true or false. true will alert a list of the images the code is looking for.
    	var defaultImage = 'default.gif';
    	var events = new Array(12);
    	for (var i = events.length - 1; i > -1; --i){
    		events[i] = new Array(31);
    		for (var j = events[i].length - 1; j > -1; --j){
    			events[i][j] = {};
    		}
    	}
    	events[3][14] = {link: 'http://www.google.com/', text: 'Google'};
    
    ////////////////////// No Need to Edit Below Here //////////////////////
    
    	var d = new Date(), dm = d.getMonth() + 1, dd = d.getDate(), dy = d.getFullYear(),
    	files = [
    		[dm, dd, dy].join('-') + '.jpg',
    		[dm, dd].join('-') + '.jpg',
    		dm + '.jpg',
    		defaultImage
    	], file, months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    	function loadError(i){
    		var img = new Image();
    		img.onload = function(){
    			document.getElementById('dailyimage').src = this.src;
    			document.getElementById('dailylink').href = events[dm][dd].link || 'http://en.wikipedia.org/wiki/' + months[dm - 1] + '_' + dd;
    			document.getElementById('dailylink').innerHTML = events[dm][dd].text || 'Wiki Date: ' + months[dm - 1] + ' ' + dd;
    		};
    		img.onerror = function(){
    			if(++i < files.length){loadError(i);}
    		}
    		img.src = folder + files[i];
    	}
    	loadError(0);
    	if(diag){alert(folder + files.join('\n' + folder));}
    })();
    </script>
    </body>
    </html>
    What this does is create an array of the 12 months, each with 31 days in it. I know that's unrealistic, but any days that don't exist will be ignored by the script because it will never look for them. The red line shows a typical entry for a date you want. You can have as many or as few of these as you like. They don't have to be in order, but that will help you keep track of them. The month numbers are human month numbers, not javascript month numbers, so 3 is March. 14 is the date. The link: property will be the href and the text: property will be what the link text will say. If there's a date you don't have configured, the href will default back to the wiki date page for that date and the text to:

    Wiki Date: March 14

    with the month and date corresponding to the current date.



    Added later - An example of adding another entry is:

    Code:
    <script type="text/javascript">
    
    // Daily Image Script (c)2012 John Davenport Scheuer
    // as first seen in http://www.dynamicdrive.com/forums/
    // username: jscheuer1 - This Notice Must Remain for Legal Use
    
    ;(function(){
    	var folder = 'images/'; // set image folder
    	var diag = true; // true or false. true will alert a list of the images the code is looking for.
    	var events = new Array(12);
    	var defaultImage = 'default.gif';
    	for (var i = events.length - 1; i > -1; --i){
    		events[i] = new Array(31);
    		for (var j = events[i].length - 1; j > -1; --j){
    			events[i][j] = {};
    		}
    	}
    	events[3][14] = {link: 'http://www.google.com/', text: 'Google'};
    	events[3][15] = {link: 'http://www.dynamicdrive.com/', text: 'Dynamic Drive'};
    
    ////////////////////// No Need to Edit Below Here //////////////////////
    Last edited by jscheuer1; 03-15-2012 at 04:06 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Sincerest thanks

    Hi John

    My sincerest thanks and gratitude goes out to you, this is precisely what I was looking for all along and you've realized it for me, it works like a dream.

    Cant thank you enough!

    Bless

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •