Can anybody tell me how I can access adsense ads with javascript? For example, if I wanted to add a click handler to each of the ads, is this possible and how would I go about this?
I think it's can be not? ads displayed in a new window (frame), so you can't handle them, visit my Javascript library for examples
i've tried to do this also but since they are embedded into an iframe on a remote host, there are XSS security restrictions in place that disallow usage of selectors from the parent domain. i then tried to setup events on $("google_ads_frame1") and hope to do some event bubbling magic but failed miserably - the mouseenter/leave works, although w/o feedback on what's underneath. the click does not even fire locally... if you find a way around it, let me know
I think i have worked it out... it seems to work fine for me. The only way it will record a false click is if the user sets the focus on the ad (using the TAB key), and then exits the page within the next 10 seconds... so i doubt that will happen very often. I'll post the whole script including the server-side .net for anyone that is interested. I didn't want to use a database to keep it simple, so clicks are recorded in a simple comma-seperated format like: URL,Ad_ID,Nf_Clicks The whole script consists of four files and are as follows: clickrecorder.js (include on all pages): window.onload = function() { var beenClicked; var myFrames = document.getElementsByTagName("iframe") for(i=0; i<myFrames.length; i++) { if(myFrames[i].name.substr(0,6)=="google") { myFrames[i].onfocus = handler; } } function handler() { beenClicked = this.id; setTimeout("beenClicked=null;", 10000); } function sendclick(adId) { var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET","recordclick.aspx?adId=" + adId,false); xmlHttp.send(null); } window.onunload = function() { if(beenClicked!=null) { sendclick(beenClicked); } } } Code (markup): recordclick.aspx: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="recordclick.aspx.vb" Inherits="recordclick" %> Code (markup): recordclick.aspx.vb (code-behind): Imports System.IO Partial Class recordclick Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Response.Expires = -1 Dim ref As String ref = Request.ServerVariables("HTTP_REFERER") If Request.QueryString("adId") IsNot Nothing And ref <> "" Then Dim sr As New StreamReader(Server.MapPath("googleadcount.txt")) Dim thisLine, adId, strDoc As String Dim cnt As Integer = 0 adId = Request.QueryString("adId") Do While Not sr.EndOfStream thisLine = sr.ReadLine Try If thisLine.Split(",")(0) = ref And thisLine.Split(",")(1) = adId Then cnt = CInt(thisLine.Split(",")(2)) cnt += 1 strDoc += ref & "," & adId & "," & CStr(cnt) & vbCrLf Else strDoc += thisLine & vbCrLf End If Catch ex As Exception End Try Loop sr.Close() If cnt = 0 Then strDoc += ref & "," & adId & ",1" & vbCrLf End If Dim sw As New StreamWriter(Server.MapPath("googleadcount.txt"), False) sw.WriteLine(strDoc) sw.Close() End If End Sub End Class Code (markup): googleadcount.txt (blank text file)
unfortunately mozilla doesn't support the onfocus event for iframes.... so it will not work in firefox... currently.
this will record the iframe id? in my case, my iframe can contain 1 or 4 adverts in any case, i wanted to hijack the behaviour for mouseover/click so I can style the adverts as well as record clicks out and I just could not get it to work in firefox in any meaningful shape or form... out of curiosity, isn't onbeforeunload a better option - doesn't onunload tend to fire a little too late, the browser transition can interrupt the ajax call...
I wasn't sure if all browsers supported onbeforeunload ... but this will only work in I.E anyway so i guess it doesn't matter. Yeh it will not record what ad exactly is clicked but can still give a rough idea... I'm going to try and work on something better but this will be a start. Browser security is a bit of an issue, in that you cannot alter the contents of an iframe that contains a page from a different domain.
http://ajaxian.com/archives/cross-domain-iframe-communication-without-location-polling - interesting post yesterday on the subject of cross domain iframe communication