JavaScript to find your Yahoo! ranking

April 19th, 2006. Tagged: JavaScript, JSON, yahoo

Motivation

Inspired by this article on SitePoint that shows how to find the Google ranking for a specific page and a search query, I decided to do the same, but for the Yahoo! ranking. The fun part is that my script is a JavaScript and requires nothing but a browser in order to run.

How mine is different

In the article above you need to use Google's SOAP service, so if you're not lucky enough to be running PHP5, you'll probably need something along the lines of PEAR SOAP or NuSOAP. That implies you also need a web server, running PHP. Then you need a Google API key and you need to download stuff and upload it to your server.

Nothing even close to that in terms of requirements if you opt in for the Yahoo! web service. All you need is a browser and JavaScript enabled, which shouldn't be a big deal, I don't think ;)

About the Yahoo! JSON web service

Yahoo's web service can return XML as everybody else, but it can also return serialized PHP and also JSON. Using the JSON option you can make a simple XMLHTTPRequest and get all the content JavaScript-ready, without the headaches of getELementsByTagName() or other DOMmy methods to wrestle that XML tree. The problem here is that you're requesting a file from a different domain, so the browser won't allow it. Workaround - a simple PHP script to serve as a proxy. Oooor (as we said we don't need no stinkin' server) you can use the dynamic JavaScript includes (discussed here) to do the request. As a result you get a working solution with JS only.

By the way, if you're wondering about the beauty of JSON, try this eye-opener.

Demo

Ah, yes, the demo is here.

Enter a/ your URL, or part of it, and b/ a search query. Then the script will tell you where in the first 1000 results is your URL to be found. If it is found.

How it works

Check the source for the details, it's reasonably well commented, but the big picture:

  • You make a request (in yjsonrank.makeRequest()) by appending a new SCRIPT element to the HEAD of your HTML. The URL of the script element (the SRC attribute) points to the Y! web service and also passes the search query and a function to be called once the script is included. This function happens to be yjsonrank.process()
  • The yjsonrank.process() function receives JSON data returned by the service, assigned to the resp variable.
  • We loop through resp.ResultSet, checking every resp.ResultSet.Result if its Url property contains our URL. If yes - we're done! If not, we make another request this time for the next 50 results. (50 is randomly chosen, feel free to modify). We continue until we reach 1000th result, which is the max that Y! will be willing to give.

And that's pretty much it, the rest is just fluff and beautifications ;)

More Y! info

Thanks for reading!

Tell your friends about this post: Facebook, Twitter, Google+

Sorry, comments disabled and hidden due to excessive spam. Working on restoring the existing comments...

Meanwhile, hit me up on twitter @stoyanstefanov