Google 和 Yahoo 如何替换浏览器状态栏中的 URL?
在Google和Yahoo搜索页面上,10个搜索结果链接的URL实际上指向google.com或yahoo.com。 URL 具有额外的参数,允许 google.com 或 yahoo.com 在单击链接时重定向到实际搜索结果。当用户将鼠标悬停在链接上时,搜索结果 URL(而不是 google.com 或 yahoo.com URL)将显示在浏览器的状态栏中。
我想知道他们是怎么做到的。
许多年前,这可以通过一些设置 window.status 的 javascript 来完成,但这似乎不再起作用,正如 设置状态栏文本的可靠跨浏览器方式
我有一个如下所示的链接: Click Me
此链接尝试使用 window.status 策略,但不起作用。如何修复此链接,使其与 Google 和 Yahoo 搜索结果页上的链接一样?在此示例中,我希望当用户将鼠标悬停在链接上。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
很难阅读源代码,但您会发现实际上 URL(在
标记中)是正确的目标 URL,这就是为什么浏览器的状态栏显示正确的 URL (而不是当您实际点击时它会重定向您的跟踪链接)。然后,一些
onclick
JavaScript 可以在浏览器的默认操作(跟随链接)发生之前拦截点击。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 someonclick
JavaScript that can then intercept the clicks before the browser's default action (following the link) can take place.Google 在每个链接上都有 onMouseDown 处理程序,可将链接从原始源更改为指向 Google 重定向。因此 onmousedown 替换了链接,当
onClick
出现时(onmousedown 之后不久),链接已经指向原始方向之外的其他位置。步骤 1. 用户单击链接(鼠标按钮按下)
步骤 2. onMouseDown 事件触发
步骤 3. 链接目标(href 值)更改
步骤 4. 鼠标按钮出现
步骤 5. onClick 事件触发
步骤 6. 浏览器看到单击链接并将用户转发到所需的目的地(由已更改的 href 值设置)
步骤 7. 浏览器打开 Google 重定向页面,这会将用户转发到原始目的地
更新:Google 用于跟踪 onmousedown 上的点击仅事件,并没有改变链接目的地。当在链接上按下鼠标按钮时,会向 google 服务器发出图像加载请求,该服务器会计算点击次数 (
onmousedown => new Image("coogle.counter.server.com/link=www.pressed.com" )
) 但我猜它被误用了或者不够可靠,所以他们决定使用当前的链接更改技术。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
onClick
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.我认为他们实际上在链接的
href
中有完整的链接。但他们使用 JavaScript 来捕获onclick
,然后当您单击该链接时,它会通过他们的网站进行路由。I think they actually have the full link in the
href
of the link. BUT they use javascript to catch theonclick
and then when you click the link, it routes through their site.例如,StackOverflow 的链接实际上看起来像这样:
现在,单击函数位于最小化源代码内部的某个位置。这里的代码带有一些额外的空格:
无需真正详细查看它,它的重要思想是它计算一些 google url,并设置
this.href
(= 链接的链接目标! ) 当您单击链接时指向该新网址。之后,将对链接进行评估,浏览器会将您发送到更改后的 url,尽管之前显示的是原始链接 url。For example the link to StackOverflow looks actually like this:
Now the click function is somewhere inside that minimized source code. Here you have the code with some additional whitespace:
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.这是一个多部分的过程。对于给定的
标记,HTML 中的
href
属性将指向实际页面。这使得没有 JavaScript 的浏览器能够转到正确的位置。接下来,链接上有一个
mousedown
事件处理程序。当您将鼠标悬停在链接上时按下鼠标按钮时,将触发 mousedown 事件。即使按下鼠标右键或中键,该事件也会触发。该处理程序将href
替换为搜索引擎域中的重定向脚本。这样,他们仍然会在最后可能的时刻显示正确的 URL,但他们仍然使用重定向命中记录器,即使您在新选项卡中打开链接也是如此。
It's a multipart process. For a given
<a>
tag, thehref
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 thehref
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.
看来它们的做法与您在示例中的做法完全相反。他们有 href="the link" 和 onclick 事件作为跟踪功能。
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.