JavaScript to find your Yahoo! ranking

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


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.


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+

11 Responses

  1. [...] It’s your responsibility to die() if necessary… – PHP Manual « JavaScript to find your Yahoo! ranking [...]

  2. Aah, very nice JSON implementation, Stoyan!
    /me likes! :-)

  3. That is a pretty interesting checker script you have there. I’m going to try a few of my sites just for fun. Cool…thanks a lot.

  4. That is in all probability the most effective article that ever cross my reference. I don’t see why anyone ought to disagree. It could be too easy #for them# to comprehend…anyway nice work i’m coming again right here for More Nice Stuff!!


  6. Value info. They must be something best. Thanks

  7. Nice post. Totally agree with her.

  8. Really great info. I bookmarked it. Great work! Thanks :)

  9. I think everybody will love to examine this article again & once more and am quite sure that most visitors of this page will come here once more in future.I expect such informative articles from you.Thanks.

  10. Nice article!

  11. Hey there! This post could not be written any better! Reading this post reminds me of my old room mate! He always kept chatting about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!

Leave a Reply