Question

Google sets a HTTP Referrer when someone clicks on a search result link. This referrer is different to the address which is shown in the URL address bar (e.g. it contains the GET parameter cd which indicates the search result position of the clicked link).

It seems that this is done with javascript. I would like to do something similar on my website, so I would be interested to know how exactly this "referrer manipulation" can be done with javascript.

Any ideas?

Was it helpful?

Solution

Google redirects you through its own click-proxy when you click on its result links - the address of the proxy is what you see in Referer.

Example: I went to http://www.google.cz and searched for 'How does Google set the HTTP Referrer when someone clicks on a search result link?'. That resulted in this request:

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/

From there, I clicked the first result link: How does Google set the HTTP Referrer when someone clicks on a search result link? That click was captured by a JS event and rerouted to this redirector:

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

containing this redirect snippet:

<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>

which finally sent me to the real 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

So, you're correct - most of the heavy lifting here is done through JavaScript, although some server-side code is also involved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top