Embed javascript in V3 InfoWindow

169 views
Skip to first unread message

John James

unread,
Mar 16, 2011, 10:09:06 AM3/16/11
to google-map...@googlegroups.com
I've been digging for a while and cannot figure this, so...

I want/need to embed a snippet of JavaScript in an InfoWindow, to dynamically create a mailto: link (as a simple means of protection against email address harvesters).  

I've tried embedding a <script>...</script> in the content string which calls a function that generates and then document.writes the 'mailto:' to the InfoWindow, but this seemed to be ignored. 

I've also tried defining <span id="emailaddr">...</span> in the content, then executing document.getElementById('emailaddr').innerHTML=emailAddress(); as the last statement in the Listener function defined on the Marker (i.e. after the InfoWindow has been displayed). The function emailAddress() constructs and returns the 'mailto: link and returns it.  This also did nothing.

The current page is at www.la-lupa-3.co.uk/find.html.  The function that I was originally trying to invoke can be found here www.la-lupa-3.co.uk/scripts/email.js
The JavaScript to set up the map, InfoWindows/Markers etc is at www.la-lupa-3.co.uk/scripts/map.js

Is it possible to embed JavaScript in the content of an InfoWindow, and have it execute when the InfoWindow is displayed, in this way?  If not, is there another technique that will achieve what I want, or am I left hard coding the email address in the content string?

Any constructive help/advice gratefully received.  I'm developing/testing in Chrome 10.0.648.133, but any solution needs to be cross-browser.

Thanks,
John 

Chris Broadfoot

unread,
Mar 17, 2011, 4:07:05 AM3/17/11
to google-map...@googlegroups.com, John James
I'd take a different approach. Generate the HTML *before* you call setContent on the InfoWindow.

Example (not real code, just a demonstration on what I'd do:

var infowindow = new google.maps.InfoWindow();

function onClick(marker) {
  infowindow.setContent('<a href="' + generateEmail(marker.getTitle()) + '">Email!</a>');
  infowindow.open(marker, map);
}

function generateEmail(name) {
  return 'mailto:' + name + '@google.com';
}

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.



--
http://twitter.com/broady

John James

unread,
Mar 17, 2011, 5:39:18 AM3/17/11
to google-map...@googlegroups.com
Chris, thanks for your reply but, unless I'm missing something, that doesn't solve the issue I'm addressing as the email address is then hard-coded in the HTML sent to the browser and is therefore available to harvesters.  Whereas if the JavaScript sent to the browser dynamically writes the HTML for the mailto: at the client end, the email address is not (or at least far less) available to the harvester (unless the harvester can strip out and eval() the JavaScript and then harvest the results, of course).

Your solution is what I'm left doing now, but I want to be able to hide the email address.  The fundamental question is whether JavaScript can be embedded and executed in an InfoWindow, irrespective of the particular application.

Chris Broadfoot

unread,
Mar 17, 2011, 8:32:15 AM3/17/11
to google-map...@googlegroups.com, John James
It doesn't look like you're missing anything. I had a look at the code and the approach you've taken is how I'd do it.

Create a string with appropriate HTML, and then use it as the parameter to setContent.

I can't comment on whether this is effective against spam bots, but the two techniques are comparable.

As for the need to add script tags inside InfoWindow content - there is a better way to approach such problems, and you've already found it.

Chris

--
http://twitter.com/broady
Reply all
Reply to author
Forward
0 new messages