View Single Post
Old 01-29-2013, 05:23 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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()
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
snarf1974 (02-05-2013)