if (document.all && window.attachEvent) { window.attachEvent("onload", initialize2); // Non-IE load and unload } else if (window.addEventListener) { window.addEventListener("load", initialize2, false); }function setUserPos(position) { var getcity = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); getcity.geocode({'latLng': latlng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { document.getElementById('fromAddress2').value = results[1].formatted_address; } } else { alert("Geocoder failed due to: " + status); } }); } navigator.geolocation.getCurrentPosition(setUserPos);