views:

303

answers:

6

On the Google and Yahoo search pages, the URLs of the 10 search result links actually point to google.com or yahoo.com. The URLs have extra arguments that allow google.com or yahoo.com to redirect to the actual search result when the link is clicked. When the user mouses over the link, the search result URL (and not the google.com or yahoo.com URL) is displayed in the browser's status bar.

I'm wondering how they do that.

Many years ago, this would have been accomplished by having some javascript that sets window.status, but that doesn't seem to work anymore, as is explained by http://stackoverflow.com/questions/876390/reliable-cross-browser-way-of-setting-status-bar-text

I have a link that looks like this: <a href="http://somedomain.com/ReallyLongURLThatShouldNotBeSeenInTheStatusBar" onmouseover="window.status='http://niceShourtUrl.com/'" onmouseout="window.status=''">Click Me</a>

This link tried to use the window.status strategy, but it doesn't work. How do I fix this link so that it acts like the links on Google's and Yahoo's search result pages? In this example, I want "http://niceShourtUrl.com/" to be displayed in the status bar when the user mouses over the link.

+9  A: 

It's hard to read the source, but you will see that in fact the URLs (in the <a> tags) are the correct destination URLs, which is why the browser's status bar shows the correct URL (instead of the tracking link that it redirects you through when you actually click). There is then some onclick JavaScript that can then intercept the clicks before the browser's default action (following the link) can take place.

Adam Batkin
So, I'm guessing that they don't track right-click > open in new tab or middle-click
Atømix
@Atomiton Of course they don't "track" it. The browsers implementation of Javascript treats it as a legitimate click and fires the event handler.
Josh Stodola
@Josh: Would it? Doesn't `right-click > open in new tab` simply copy and paste the href into a new tab's address bar? I can't remember any javascript events firing in this case. EDIT: Ah! mousedown event traps all buttons... left, right and middle.
Atømix
@Atomiton Yeah I think I am mistaken. I thought for sure it would fire onclick!
Josh Stodola
@Josh. Yeah, it makes sense to trap mousedown, though.
Atømix
+6  A: 

I think they actually have the full link in the href of the link. BUT they use javascript to catch the onclick and then when you click the link, it routes through their site.

webdestroya
Yes, they use scripting to record some unique ID for that URL for tracking, but it's still an actual link to the actual page. @Mike W: Did you try using Firebug to look at the source code? You can see the actual href link points to the page, not a Google page.
animuson
+2  A: 

For example the link to StackOverflow looks actually like this:

<a onmousedown="return clk(this.href,'','','res','1','','0CBwQFjAA')" class="l" href="http://stackoverflow.com/"&gt;&lt;em&gt;Stack Overflow</em></a>

Now the click function is somewhere inside that minimized source code. Here you have the code with some additional whitespace:

window.clk = function ( e, f, g, k, l, b, m )
{
    if ( document.images )
    {
        var a = encodeURIComponent || escape,
            c = new Image,
            h = window.google.cri++;

        window.google.crm[h] = c;
        c.onerror = c.onload = c.onabort = function()
        {
            delete window.google.crm[h]
        };

        var d, i, j;

        if ( google.v6 )
        {
            d = google.v6.src;
            i = google.v6.complete || google.v6s ? 2 : 1;
            j = (new Date).getTime() - google.v6t; delete google.v6
        }

        if ( b != "" && b.substring( 0, 6 ) != "&sig2=" )
            b = "&sig2=" + b;

        c.src = [
                "/url?sa=T",
                "&source=" + google.sn,
                f ? "&oi=" + a(f) : "",
                g ? "&cad=" + a(g) : "",
                "&ct=",
                a( k || "res" ),
                "&cd=",
                a( l ),
                "&ved=",
                a( m ),
                e ? "&url=" + a( e.replace( /#.*/, "" ) ).replace( /\+/g, "%2B" ) : "",
                "&ei=",
                google.kEI,
                d ? "&v6u=" + a( d ) + "&v6s=" + i + "&v6t=" + j : "",
                b ].join( "" )
    }
    return true
 };

Without really looking at it in detail, the important idea about it is that it calculates some google url, and sets this.href (= the link's link target!) to that new url when you click the link. After that the link is then evaluated and the browser sends you to that changed url despite showing the original link url before.

poke
I don't see anywhere in this function where it's actually changing 'this.href', it's only sent to the function via the initial call... To me it looks like this function changes the source of an image somewhere.
animuson
+4  A: 

Google has onMouseDown handlers on every link that change the link from the original source pointing towards Google redirect. So onmousedown replaces the link and when onlick appears (shortly after the onmousedown) the link is pointing already to somewhere else than the original direction.

Step 1. User clicks on a link (mouse button is down)

Step 2. onMouseDown event triggers

Step 3. link target (a href value) is altered

Step 4. Mouse button comes up

Step 5. onClick event triggers

Step 6. Browser sees that a link was clicked and forwards the user to the desired destination (set by an already altered href value)

Step 7. Browser opens a Google redirect page and this forwards the user to the original destination

Updated: Google used to track clicks on an onmousedown event only and didn't alter the link destination. When a mouse button was pressed on a link an image loading request was made towards google servers which counted the click (onmousedown => new Image("coogle.counter.server.com/link=www.pressed.com")) but I guess it was misused or it wasn't reliable enough that they decided to use the current link altering technique.

Andris
No, it doesn't. If you look closely at the code that was posted below, you'd see that the href is never actually changed.
animuson
Yes it does - it depends which function is currently used - clk() or rwt(). rwt() does exactly what I described.
Andris
And actually clk changes the href also
Andris
The Google page I loaded had the clk() function, and where in that function do you see it changing the href?
animuson
Look on the top of the code sample: <a onmousedown="return clk(**this.href**, ...
Andris
The function returns true, which will cause that to return true, which allows the anchor to redirect to the specified href. If that returned false, nothing would happen. 'this.href' is just a *parameter* being *sent* to the function, it's not being rewritten.
animuson
sorry, misinterpreted it uses this.href as a url source
Andris
Anyway, when I look at a google page, then it doesn't use clk() byt rwt() to "rewrite" the url destionations. So maybe people behind different nodes get different behavior. The clk() function looks more like the "updated" section in my post above
Andris
A: 

It's a multipart process. For a given <a> tag, the href attribute in the HTML will point to the actual page. This allows browsers without JavaScript to go to the right place.

Next, there is a mousedown event handler on the link. The mousedown event fires when you depress a mouse button while hovering over the link. This event fires even if the right or middle mouse button is pressed. The handler replaces the href with the redirecting script in the search engine's domain.

That way they still display the correct URL up to the last possible moment, but they still use the redirecting hit logger, even when you open the link in a new tab.

jimbojw
Ah... this answers my question about right-click. Though, I suppose that would also log all the context-sensitive menu-clicks like `save link` `bookmark link` `send link`... but I suppose Google probably runs a statistical analysis and applies a percent click-through to the right-click results.
Atømix
A: 

It appears they do the direct opposite of what you have in your example. They have the href="the link" and the onclick event as the tracking function.

Daniel