How does URL replacement on the google SERPs "sponsored links" work?

Some of the results have a function rwt attached to the onmousedown event, but the sponsored links don't have any event handlers as far as I can make out. How does it achieve the behaviour of replacing the href, and still send you to the long /aclk?sa= link?

Upon saving to a local file, the javascript magically breaks foiling my own attempts to pick it apart. Hopefully someone could give me a starting point of attack.

Just a few other observations. There is a regular expression chopping the q= part off the URL, but I can't for the life of me see how they "reset" the URL so it can be tracked. Also the replacement only worked properly under firefox. IE7 showed a blank status bar. Safari and opera showed the ugly URL.

--update-- Thanks for having a go Carl but I think you were closer the first time. I guess I didn't explain it at all well. I'm trying to figure out how the javascript on the SERPs page works. Not the server side logic.


I think I understand what you are asking, if I'm off base, my apologies:

Are you sure you were viewing the generated source code? I've found that Google will, sometimes, render the links, and then add an onclick event after the page loads that rewrites the url during the onclick event. If you click a link (that looks un-rewritten) then go back to the search results (by clicking "back" on the browser) and hover over the same link, you may find that the link is now rewritten in the status bar.

Does that make sense?


I think I misunderstood your question... After reading it, plus your comment, I think I understand what you were asking.

So I did a search on for "java", and hovered over the top "Sponsored Link", and it was as such:[snipped] So you're wondering how they then transfer you to the right url after that? If that is indeed the question, then the answer would be that they store information about that sponsored link in their db, and pass that information to the search results page. When you click the link, you're sent to a "sponsored link" handler, which looks up the info that was passed to it in the long url seen above, and it tracks it as a click, then, it looks up what the resulting URL is supposed to be, and redirects you there. If you were to use a tool like Fiddler (for IE) you would probably see what was happening on your end more clearly. It's similar to how works. I go there, give it a long url, and it spits out a shorter one. It's then associating that tiny code to the url I entered. So, for example, I entered in, and it spit out "2tx" will be used to redirect me to

Was I closer to the mark this time? I feel as though I might not be :-/

Clicking, and then going back didn't effect the link in the way you said, as if it had been done with an onlick. Actually the only event handler in the script section is document.onclick=b.closeObviously the soup of single char variables isn't helping but I'd love to understand what's going on here
you'd love to understand they they are doing their damned best to ensure you don't.
I agree with Unkwntech :)