Jump to content

Going global with variables.


Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

Considering that this guide is posted in the official firebug website, everything said there must be correct.

Link to post
Share on other sites

Firebug is good. If you use Chrome/Safari (webkit), IE, Opera you have Developer Tools built in as well.

  • Like 1
Link to post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...