Talk is cheap, show me the code
I’m dragged so far away from my early intention (doing similar blog ranking using Google API). I thought PHP will be sufficient. But unfortunately I was wrong. Timeout issue hit me hard. 456 urls really made script execution took a while. This is obviously beacuse sync issue. The request to my broker (to fetch google search result count via web service) was made in a serial manner. I have a hunch that async method will be appropriate solution. This is where Ajav jumped in.
I made a huge mistake in my first attempt. I thought the OnReadyStateChange event hander will be able to deal with the multi request independently. It looked like only last request result which was rendered onto the page. Darn, I should have ben doing it in thread-ish way. But how the f*** am I going to do that?
Uhmm, why don’t we make the resuest happens inside an object. Thus each onReadyStateChange event will have its own private event handler. Brilliant.
OMG, this is a joke. OnReadyStateChange event handler cannot be wrapped inside an object. It does receive private handler but it will be executed in a global context, thus makes all object methods and props become unavailable. And, as you may have guessed, putting the event handler in global context drops me back on to square one.
function Blog(url, blogCollection) {
this.blogs = blogCollection;
this.url = url;
}
Blog.prototype = {
url: "",
result: 0,
blogs: [],
makeRequest: function() {
//debug(’Checking on Google: ‘ + url);
//alert(url);
var url = ‘blog.php?blog=’ + this.url;
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,…
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType(’text/xml’);
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject(”Msxml2.XMLHTTP”);
} catch (e) {
try {
http_request = new ActiveXObject(”Microsoft.XMLHTTP”);
} catch (e) {}
}
}
if (!http_request) {
alert(’Giving up
Cannot create an XMLHTTP instance’);
return false;
}
http_request.onreadystatechange = this.alertContents;
http_request.open(’GET’, url, true);
http_request.send(null);
},
alertContents: function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(this.url);
var xmldoc = http_request.responseXML;
processXML(xmldoc);
} else {
alert(’There was a problem with the request.’);
}
}
}
}
function bar() {
alert(”outside object”);
}
function getBlogs(x) {
var s = “
“;
return s;
}
function compareBlog(a, b) {
return a.result - b.result;
}
function processXML(xml) {
var node = xml.getElementsByTagName(”url”).item(0);
var url = node.firstChild.data;
var node = xml.getElementsByTagName(”result”).item(0);
var result = node.firstChild.data;
alert(result);
// push into global var
//a.push(blog);
for(var i=0;i
if (a[i].url==url) {
a[i].result = result;
alert("Found: " + blog.url + " set result to: " + blog.result);
return;
}
// render results
a.sort(compareBlog);
document.getElementById("foo").innerHTML = getBlogs(a);
}
function foo(x) {
for(var i=0;i
//x[i].test();
//alert(x[i].url);
}
var a = new Array();
a.push(new Blog("http://100kata.blogspot.com/", a));
a.push(new Blog("http://1012.blogspot.com/", a));
a.push(new Blog("http://13katanya.blogspot.com/", a));
Sphere: Related Content
This section gives me most headache. But experience told me that this blog mostly contains personal ramblings related to daily life, open source, and web 2.0. Recently, it turns out to be an idea-box where you can found ideas you can execute in your new startup ;). My ideas and opinions are not bullet proof and never intended to be one way stream. Kindly participate to improve your and my perspective regarding any particular post. Welcome aboard and enjoy your stay
Leave a reply