View Single Post
Old 01-30-2013, 04:26 AM   PM User | #9
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,456
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
the URLs and classnames you provided are a bit out of whack, i suspect local/live drift as the culprit.

nonetheless, the following code ran in chrome's inspector from: http://www.hse.gov.uk/horizons/index.htm .



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("/horizons/sfreports.htm", "ul.itemThumbBook", ".hse2ColumnRight", 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()
i didn't see a ul#tips anywhere, so i did the best i could. kinda just threw it into a container at the bottom...

but now that it's operational, you should be able to adjust the css selectors to match the actual markup in use without much fuss. remember that the paths and css selectors have to match up, and that you can't pull the live page from your test rig, you have to pull the local copy, which i observed to be on a different url path than indicated.

the gun is firing. as long as it's pointing in the right place, it will hit the target.
__________________
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)