Simple Quotes Widget
HTML
<body>
<div id="titlebkgrnd"></div>
<div id="titletext">Commodities</div>
<div id="titleaccent"></div>
<div id="blk-1" class="current">
<table class="table"> </table>
</div>
</body>
CSS
#titlebkgrnd {
background-color: #103346;
height: 28px;
width: 305px;
}
#titletext {
color: #ffffff;
font-size: 15pt;
position: fixed;
top: 11px;
left: 18px;
}
#titleaccent {
background-color: #C3A43F;
height: 6px;
width: 305px;
}
.my-new-list {
font-size: 13pt;
}
#name{
font-weight: bold;
}
.cells{
padding-right: 12px;
}
.table{
padding-top: 12px;
}
/* These two classes, .red & .green are used to change the font color of values
* depending on the symbol that preceeds the value (+ or -)
*/
.red { color: red; }
.green { color: green; }
JavaScript
//This function makes the request to the Yahoo API for symbols GCF14.CMX, SIF14.CMX, PAH14.NYM, PLF14.NYM
//To create the link for the query, the YQL console was utilized
$(function () {
$.getJSON('http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.finance.quotes%20WHERE%20symbol%20in(%22GCF14.CMX%22%2C%22SIF14.CMX%22%2C%22PAH14.NYM%22%2C%22PLF14.NYM%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=').done(function (data) {
console.log("data: ", data);
console.log(data.query.results.quote);
$.each(data.query.results.quote, function (key, obj) {
var changeInPercentClass = '';
var commodityValueChange = obj.ChangeRealtime;
var commodityTableRow = document.createElement("tr");
var commodityTableCell = document.createElement("td");
commodityTableRow.setAttribute("class", "my-new-list");
commodityTableRow.append($('<td id="name" class="cells"/>').text(obj.Name.split(' ')[0] || "--"));
commodityTableRow.append($('<td class="cells"/>').text(obj.AskRealtime || "--"));
(obj.Change.substr(0,1) === '+') ? changeClass = 'green' : changeClass = 'red';
(obj.Change.substr(0,1) === '+') ? changeInPercentClass = 'green' : changeInPercentClass = 'red';
if (typeof commodityValueChange === "string") {// && /^-?(\d\.?|\d*\.\d+)$/.test(commodityValueChange)) {
commodityTableCell.textContent = Number(commodityValueChange).toFixed(2);
}
else {
commodityTableCell.textContent = "--";
}
commodityTableRow.appendChild(commodityTableCell);
var re = /([+|-]\d\.\d\d\%)/gi;
var rt = re.exec(obj.ChangePercentRealtime);
commodityTableRow.append($('<td class="'+changeInPercentClass+'">').text( rt[0] || "--"));
});
});
});