简体   繁体   中英

Code not executing when referred to in Javascript file

I am using geoip-db.com to identify the general location a user. My code works on its own, however im facing a bit of difficulty integrating it with my other code.

I am using the following bit of code to identify the location:

 var country = document.getElementById('country'); var state = document.getElementById('state'); var city = document.getElementById('city'); var postal = document.getElementById('postal'); var latitude = document.getElementById('latitude'); var longitude = document.getElementById('longitude'); var ip = document.getElementById('ipv4'); function callback(data) { country.innerHTML = data.country_name; state.innerHTML = data.state; city.innerHTML = data.city; postal.innerHTML = data.postal; latitude.innerHTML = data.latitude; longitude.innerHTML = data.longitude; ip.innerHTML = data.IPv4; } var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://geoip-db.com/json/geoip.php?jsonp=callback'; var h = document.getElementsByTagName('script')[0]; h.parentNode.insertBefore(script, h);
 <div>Country: <span id="country"></span></div> <div>State: <span id="state"></span></div> <div>City: <span id="city"></span></div> <div>Postal: <span id="postal"></span></div> <div>Latitude: <span id="latitude"></span></div> <div>Longitude: <span id="longitude"></span></div> <div>IP address: <span id="ipv4"></span></div>

I would like the output to be through the following code (We will call this code A):

 case 'send end': if (connect === 1) { $('#terminal').append('<div>C:/root/anon> ' + $(this).val() + '<br><br>Country: <span id="country"></span></div><div>State: <span id="state"></span></div><div>City: <span id="city"></span></div><div>Postal: <span id="postal"></span></div><div>Latitude: <span id="latitude"></span></div><div>Longitude: <span id="longitude"></span></div><div>IP address: <span id="ipv4"></span></div><span id="blinking">_</span></div>'); } else { $('#terminal').append('<div>C:/root/anon> ' + $(this).val() + '<br><br>Failed<span id="blinking">_</span></div>'); } break;

However it does not seem to work: The output is as follows:

Country:

State:

City:

Postal:

Latitude:

Longitude:

IP address:

*The lables are shown however the results are not..

This is the correct fix for Code A (I'm not sure what the event would be called in this situation, so I called it event):

function functionName (event) {
   switch (event) {
      case 'send end':
         if (connect === 1) {
            $('#terminal').append('<div>C:/root/anon> ' + $(this).val() + '<br><br>Country: <span id="country"></span></div><div>State: <span id="state"></span></div><div>City: <span id="city"></span></div><div>Postal: <span id="postal"></span></div><div>Latitude: <span id="latitude"></span></div><div>Longitude: <span id="longitude"></span></div><div>IP address: <span id="ipv4"></span></div><span id="blinking">_</span></div>');
         } else {
            $('#terminal').append('<div>C:/root/anon> ' + $(this).val() + '<br><br>Failed<span id="blinking">_</span></div>');
         }
      break;
      default:
          //This is recommended but not necessary
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM