09.22.07

AJAX closures done well

Posted in Coding at 9:31 pm by David Kellogg

First what is a closure?

A closure is a function that returns without completing itself, and which still holds local variables at their old state. It’s pretty neat and handy when you see what they can do. Here’s an example of the town of Tinyville, population 5. A new resident moves in, but the census starts before he signs a lease.

var population = 5;
function fun(count) {
 return function() {
  alert(”count is “+count);
 }
}
setTimeout(fun(population), 1000);
population++;
document.writeln(population);

Result:

6
Alert: 5

The six is written to the screen, but the old value of the population is saved by the setTimeout and the closure function, fun.

Closures are so important that reading the wrong tutorial will doom the AJAX writer. Here’s an example from Apple’s AJAX tutorial,

req.onreadystatechange = processReqChange; // DOOMED!!

req is a global variable. That’s really bad, since the chance to use a closure has past and no more than one request at a time may occur. So much for asyncronous. One alternative is to tag each outbound url, such as “http://localhost/ajax.php?population=5″. That will save the state, but that’s not as good as saving whole objects or sets of local variables. The MDC tutorial at Mozilla gets it right. Here’s the crucial line with my population variable injected.

httpRequest.population = 25;
httpRequest.onreadystatechange = function() { alertContents(httpRequest); } // BETTER

Why is this better?
You can tack on any variable to httpRequest and it will be separate from other requests. For instance “httpRequest.population=5″ is a valid way to preserve needed variables. Later the population is used.

if (httpRequest.status == 200) {
 alert(httpRequest.responseText + “population is “+httpRequest.population);
} else {
 alert(’There was a problem with the request.’);
}

One final reason to use closures is that AJAX is asynchronous, so you are not guaranteed to receive responses in the same order as the requests were dispatched. This makes using a closure especially valuable.

09.20.07

Semantic Web, please go away

Posted in Uncategorized at 11:32 pm by David Kellogg

I’m tired of hearing how the semantic web is the big thing, the meta-web, the solution to all things. It’s not. It likely will never get off the ground, and if it does, it will be of very little use. Here’s why.

Read-write has an article about how the semantic web failed so far and how should it change to gain traction. I largely agree with it. While eggheads support the semantic web, real people are getting things done without it.

The semantic web cannot succeed. I will use Read-write to prove it.

Read-write uses three HTML meta tags for this article written by Alex Iskold. One is author, which is listed as Richard MacManus. The other is description, which has a Digg link, hardly a description of anything. The third is copyright, which goes to MacManus, but not Iskold. I’ll trust the last is correct. This is typical of the web.

Unlike most semantic web supporters, I have crawled the web for 4 years, now. The first thing you learn when crawling is not to trust servers. If it says UTF-8, don’t trust it. It says it is written in Chinese, you better test it against a real language detection module. The same is true for Read-write, here. Two out of three fields are incorrect due to copying by machines.

The semantic web suffers from malice. We have a long tradition of keyword stuffing, cloaking, gateway pages and other malicious devices. Web spammers will only use the semantic web to spread disinformation. It may list Brittney Spears ringtones in the description, but the page offers viagra.

The semantic web requires a network to exist before anything else starts. Crawlers and indexers will not spend time parsing semantic web information until there is a critical mass of correctly formated pages. Site owners will not move until the indexing makes use of the semantic information.

Usually when someone trashes the semantic web, microfomat goons come out of the woodwork to sing its praises. Yet, microformats suffer from the same fraud and incompetence as the semantic web. Plus the network has to appear out of whole cloth.

So what is good, if microformats and the semantic web are doomed to failure? Freedom. Someone should have the freedom to create whatever page he wants using any method (Flash, JS, AJAX), and guys like me will have to divine its purpose. No one should have to try hard to describe a page’s purpose. Finding the purpose is the hard work that keeps me employed.

09.11.07

MyBlogLog

Posted in Coding at 3:37 pm by David Kellogg

I just joined myBlogLog. I hope it works well. So far, I cannot see the widget. Maybe it will appear soon.

OK. It appears to have worked. See the sidebar on the right. Nice.