I'm a JavaScript newbie who was first introduced to JavaScript by jQuery. I'm trying to switch over some of the small things I've written in jQuery to JavaScript as a way of learning. I was wondering how I could make my code cleaner and if there were any bad habits which I am guilty of doing.
function getTweets(user) {
//Adds a little animation to the body when function is executed
$('.container').html('<div class="loading"></div>');
var container = document.getElementsByClassName('container')[0];
container.innerHTML = "<div class='loading'></div>";
$.getJSON("https://twitter.com/statuses/user_timeline.json?screen_name=" + user + "&callback=?&count=50", function(data) {
var date = {
Sun : 'Sunday, ',
Sat : 'Saturday, ',
Mon : 'Monday, ',
Tue : 'Tuesday, ',
Wed : 'Wednesday, ',
Fri : 'Friday, ',
Thu : 'Thursday, ',
};
// Creates a box and puts JSON data into it.
for ( var i = 0 ; i < data.length; i++) {
// Makes the dates readable and links work.
data[i].text = data[i].text.replace(/(http[s]*:\/\/[\w/\.]+)/, '<a href=\"$1\">$1</a>')
day = date[ data[i].created_at.match(/\w+/)[0] ];
data[i].created_at = data[i].created_at.replace(/(\+\d+)/g, '').replace(/\w+/, day);
var art = document.createElement('article');
art.innerHTML = "<h3>" + data[i].user.name + "</h3><span class='date'>" + data[i].created_at + "</span><p>" + data[i].text + "</p>";
container.appendChild(art);
};
//Everything after this is to dynamically create a Pinterest type layout
var width = Math.floor( ( $(window).width() - 200 )/225),
col = [],
small = 0,
articleArray = document.getElementsByTagName('article');
while ( col.length < width ) {
col.push(0)
};
for (var o = 0; o < articleArray.length; o++) {
articleArray[o].style.top = col[small] + "px";
articleArray[o].style.left = small*225 + "px";
col[small] += articleArray[o].clientHeight + 5;
for ( var j = 0; j < width; j++ ) {
if (col[j] < col[small]) {
small = j
}
};
};
container.style.height = col[small] + 'px';
container.style.width = col.length * 225 - 5 + 'px';
$('.container .loading').fadeOut();
$('article').fadeIn();
});
};