I'm not sure about perfect. Nobody/nothing is. Anyways, here's how I would write it:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Load XML With jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<link href="http://cdn.net.tutsplus.com/045_LoadXMLWithJquery/StyleSheet.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.theFirst .title {
color: red;
}
</style>
<script type="text/javascript">
jQuery(function($){
/* Set Variables */
var m_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
bookdata, bookHTML = [], date = new Date(), /* curr_date = d.getDate(), */curr_date = 3, curr_month = date.getMonth(),
curr_year = date.getFullYear(), $book, title, description, imageurl, idx, firstCompleted = false;
$.get('myData.xml', function(d){
/* $('body').append('<h1> Title</h1>'); */
$('#col-a').append('<ul id="tips"/>');
bookdata = $.map($(d).find('book'), function(n){return $(n);}).reverse();
$.each(bookdata, function(i){
if(curr_month === 8 && i + 1 >= curr_date){
$book = bookdata[i];
title = $book.attr("title");
description = $book.find('description').text();
imageurl = $book.attr('imageurl');
if(firstCompleted){
bookHTML.push(['<li class="tipItem" style="list-style:none;">',
'<img class="bookImage" alt="" src="' + imageurl + '" /> ',
'<p class="title">' + title + '</p>',
'<p> ' + description + '</p>',
'</li>'].join(''));
} else {
bookHTML[0] = ['<li class="tipItem theFirst" style="list-style:none;">',
'<p class="title">' + title + '</p> ',
'<img class="bookImage" alt="" src="' + imageurl + '" />',
'<p> ' + description + '</p>',
'<p> ' + description + '</p>',
'</li>'].join('');
firstCompleted = true;
}
}
});
$('ul').append(bookHTML.join(''));
});
});
</script>
</head>
<body>
<div id="col-a"></div>
</body>
</html>
Notes: This also gets rid of any possibility that IE will have to deal with loading images being removed (something it often doesn't react well to).
There's one thing about this that disturbs me, the curr_date and how that and the curr_month relate to the selecting of what content to show. In your example you chose an arbitrary number that suited the xml file's contents for the date and hard coded the expected month # into the if statement. I've followed suit above.
It remains to be seen if, when actually using the live date, this will be workable. I'm imaging though that the value in each xml book element that is currently in the description tags, ex:
Code:
<book title="CSS Mastery" imageurl="http://cdn.net.tutsplus.com/045_LoadXMLWithJquery/images/css.jpg">
<description>
08/01/2010 - Content
</description>
</book>
could be parsed (would require at least some additional code*) in order to get things to work out in a real example of this. Doing so probably will also require tweaking of the if:
Code:
if(curr_month === 8 && i + 1 >= curr_date)
I've tried to preserve variables that are not in use but could be, as well as the commented out sections of the code from your initial example that could still be used for something. The date part for sure. I'm not certain what exactly you were going for with the <h1> Title</h1> thing. But I can imagine a few scenarios. You might want to prepend, rather than append it to the body.
*I have what appears to be a solid idea for parsing the date for the description and using it to determine which 'books' get shown.
Bookmarks