【发布时间】:2011-05-23 02:19:28
【问题描述】:
当有人点击搜索结果链接时,Google 会设置一个 HTTP 引荐来源网址。该referrer与URL地址栏中显示的地址不同(例如,它包含GET参数cd,表示点击链接的搜索结果位置)。
这似乎是用 javascript 完成的。我想在我的网站上做类似的事情,所以我很想知道这种“引荐来源操纵”究竟是如何用 javascript 完成的。
有什么想法吗?
【问题讨论】:
标签: javascript referrer
当有人点击搜索结果链接时,Google 会设置一个 HTTP 引荐来源网址。该referrer与URL地址栏中显示的地址不同(例如,它包含GET参数cd,表示点击链接的搜索结果位置)。
这似乎是用 javascript 完成的。我想在我的网站上做类似的事情,所以我很想知道这种“引荐来源操纵”究竟是如何用 javascript 完成的。
有什么想法吗?
【问题讨论】:
标签: javascript referrer
当您点击其结果链接时,Google 会通过其自己的点击代理重定向您 - 代理的地址就是您在 Referer 中看到的地址。
示例:我转到 http://www.google.cz 并搜索“当有人点击搜索结果链接时,Google 如何设置 HTTP Referrer?”。这导致了这个请求:
GET http://www.google.cz/search?hl=cs&source=hp&biw=1276&bih=866&q=How+does+Google+set+the+HTTP+Referrer+when+someone+clicks+on+a+search+result+link%3F&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=b29a84a7dd59af16 HTTP/1.1
Referer: http://www.google.cz/
从那里,我点击了第一个结果链接:How does Google set the HTTP Referrer when someone clicks on a search result link? 该点击被 JS 事件捕获并重新路由到此重定向器:
GET http://www.google.cz/url?sa=t&source=web&cd=1&ved=0CBoQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F4402502%2Fhow-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link&rct=j&q=How%20does%20Google%20set%20the%20HTTP%20Referrer%20when%20someone%20clicks%20on%20a%20search%20result%20link%3F&ei=WTgBTeOXLsHB8QPO44ybCA&usg=AFQjCNE22KabWH5TnkK1sRLGmqWQ4EvwxQ HTTP/1.1
Referer: http://www.google.cz/search?hl=cs&source=hp&biw=1276&bih=866&q=How+does+Google+set+the+HTTP+Referrer+when+someone+clicks+on+a+search+result+link%3F&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=b29a84a7dd59af16
包含此重定向 sn-p:
<body><a href="https://stackoverflow.com/questions/4402502/how-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link" id=link target=_parent></body><script>var a=parent,b=parent.google,c=location;if(a!=window&&b){if(b.r){b.r=0;document.getElementById("link").click();}}else{document.getElementById("link").click();};</script><noscript><META http-equiv="refresh" content="0;URL='https://stackoverflow.com/questions/4402502/how-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link'"></noscript>
最终将我带到了真实的 URL:
GET https://stackoverflow.com/questions/4402502/how-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link HTTP/1.1
Referer: http://www.google.cz/url?sa=t&source=web&cd=1&ved=0CBoQFjAA&url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F4402502%2Fhow-does-google-set-the-http-referrer-when-someone-clicks-on-a-search-result-link&rct=j&q=How%20does%20Google%20set%20the%20HTTP%20Referrer%20when%20someone%20clicks%20on%20a%20search%20result%20link%3F&ei=WTgBTeOXLsHB8QPO44ybCA&usg=AFQjCNE22KabWH5TnkK1sRLGmqWQ4EvwxQ
所以,你是对的 - 这里的大部分繁重工作都是通过 JavaScript 完成的,尽管也涉及一些服务器端代码。
【讨论】: