i didn't see this before, but this is a perfect use case for
my partial page content fetcher.
it's not setup to do 3 random items, so we'll need a slight modification:
changes in red:
Code:
function getPage(url, from, to, callBack) {
var cached=sessionStorage[url];
if(!from){from="body";} // default to grabbing body tag
if(cached){return elm.innerHTML=cached;} // cache responses for instant re-use re-use
if(to && to.split){to=document.querySelector(to);} // a string TO turns into an element
if(!to){to=document.querySelector(from);} // default re-using the source elm as the target elm
var XHRt = new XMLHttpRequest; // new ajax
XHRt.responseType='document'; // ajax2 context and onload() event
XHRt.onload= function() { callBack(XHRt.response.querySelector(from), to);};
XHRt.open("GET", url, true);
XHRt.send();
return XHRt;
}
// to use it with the callback:
$(document).ready(function() {
getPage("./reports.htm", "ul#tips", "ul#tips", function (elm, dest) {
var frag = document.createDocumentFragment(),
items = elm.getElementsByTagName("li");
dest.innerHTML=""; //clear list
// grab 3 different items:
for (var $i = 0; $i < 3; $i++) {
frag.appendChild(items[Math.floor(Math.random() * items.length)]);
}
// append all 3 to UL on page:
dest.appendChild(frag);
});//end getPage()
});//end ready()