sepoto Posted March 1, 2012 Share Posted March 1, 2012 I am trying to figure out how to go global with these variables. This works great: <!DOCTYPE html><html><head><title>My Title</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script>$(document).ready(function(){ $('#map').hide(); $("#theyellow").click(function() { $('#map').show();sepysfunction(); }); });//end</script><script type="text/javascript"> function sepysfunction() { //sepysfunction//sepysfunction//sepysfunction//sepysfunction//sepysfunction var mapDiv = document.getElementById('map');var latlng = new google.maps.LatLng(37.09, -95.71); var options = { center: latlng, zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP}; var map = new google.maps.Map(mapDiv, options); /****************************************************/if(document.forms[0].physichaladdress.value != "") { geocoder = new google.maps.Geocoder();var geocoderRequest = { address: document.forms[0].physichaladdress.value } geocoder.geocode(geocoderRequest, function(results, status) {/////////////////////////////////////////////////////////////if(status == google.maps.GeocoderStatus.OK) {map.setCenter(results[0].geometry.location);map.setZoom(14);getData(document.forms[0].physichaladdress.value);}////////////////////////////////////////////////////////////}); }/******************************************************/ }//sepysfunction//sepysfunction//sepysfunction//sepysfunction//sepysfunction</script> <script type="text/javascript">var thedata; function getData(str) { if (str.length==0) {return;}if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();}else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} ////////////////////////////////////////////////////////xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200) {thedata=xmlhttp.responseText;} }/////////////////////////////////////////////////////// xmlhttp.open("GET","getdata.php?q="+str,true);xmlhttp.send();}</script><style>body { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: small; background: #fff; }#map { width: 100%; height: 500px; border: 1px solid #000; }</style></head><body><form name="mymapgetter"><h3>Enter your physichal address:</h3><input type="text" name="physichaladdress" value="" style="width: 99.5%; margin-left: .25%; margin-right: .25%;"></input><input type="button" id="theyellow" value="getmap" style="width: 100%"></input></form><div id="map"></div><p>To add or comment on locations contact webmaster@wirelessinternetlocations.com</p></body></html> This however breaks the whole program: <!DOCTYPE html><html><head><title>My Title</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script>$(document).ready(function(){ $('#map').hide(); $("#theyellow").click(function() { $('#map').show();sepysfunction(); }); });//end</script><script type="text/javascript"> //GLOBALSvar mapDiv = document.getElementById('map');var latlng = new google.maps.LatLng(37.09, -95.71); var options = { center: latlng, zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP}; var map = new google.maps.Map(mapDiv, options);//GLOBALS function sepysfunction() { //sepysfunction//sepysfunction//sepysfunction//sepysfunction//sepysfunction /****************************************************/if(document.forms[0].physichaladdress.value != "") { geocoder = new google.maps.Geocoder();var geocoderRequest = { address: document.forms[0].physichaladdress.value } geocoder.geocode(geocoderRequest, function(results, status) {/////////////////////////////////////////////////////////////if(status == google.maps.GeocoderStatus.OK) {map.setCenter(results[0].geometry.location);map.setZoom(14);getData(document.forms[0].physichaladdress.value);}////////////////////////////////////////////////////////////}); }/******************************************************/ }//sepysfunction//sepysfunction//sepysfunction//sepysfunction//sepysfunction</script> <script type="text/javascript">var thedata; function getData(str) { if (str.length==0) {return;}if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();}else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} ////////////////////////////////////////////////////////xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200) {thedata=xmlhttp.responseText;} }/////////////////////////////////////////////////////// xmlhttp.open("GET","getdata.php?q="+str,true);xmlhttp.send();}</script><style>body { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: small; background: #fff; }#map { width: 100%; height: 500px; border: 1px solid #000; }</style></head><body><form name="mymapgetter"><h3>Enter your physichal address:</h3><input type="text" name="physichaladdress" value="" style="width: 99.5%; margin-left: .25%; margin-right: .25%;"></input><input type="button" id="theyellow" value="getmap" style="width: 100%"></input></form><div id="map"></div><p>To add or comment on locations contact webmaster@mydomain.com</p></body></html> I don't understand why the program breaks. The modifications I made are intended to make those variables global so they can be accessed by other functions. Does anyone know what is going on? Link to comment Share on other sites More sharing options...
thescientist Posted March 1, 2012 Share Posted March 1, 2012 are you checking for errors? are you looking in your console? It should be clear where and why if you do. my question is, why? Global variables are supposed to be avoided. Also, if you are using jquery (document.ready), all your code should ideally go in there. Link to comment Share on other sites More sharing options...
sepoto Posted March 2, 2012 Author Share Posted March 2, 2012 Thanks for setting me right. I'll try those things that you all mentioned. Link to comment Share on other sites More sharing options...
sepoto Posted March 2, 2012 Author Share Posted March 2, 2012 I found this to be a very informative article on how to use the JavaScript console here: http://getfirebug.com/logging Does anyone have some commentary on if this is the right way to go about things? Link to comment Share on other sites More sharing options...
TheGallery Posted March 2, 2012 Share Posted March 2, 2012 Considering that this guide is posted in the official firebug website, everything said there must be correct. Link to comment Share on other sites More sharing options...
thescientist Posted March 2, 2012 Share Posted March 2, 2012 Firebug is good. If you use Chrome/Safari (webkit), IE, Opera you have Developer Tools built in as well. Link to comment Share on other sites More sharing options...
rfps Posted March 5, 2012 Share Posted March 5, 2012 A tip, to create a really global var on javascript no initialize with var. For example: Local var is:var x = 'something'; Global var:x = 'something'; On function use this to refer the scope: this.x = 'something'; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.