but i’d like to make the location into a global variable so i can pass it on to other functions. I tried creating it outside the function, didn’t work. Can I get some advice as to what I am doing wrong.
thx
If you wrap all your separate scripts in IIFEs then the only globals will be the ones passing the library to the script.
You can then define all the variables that the particular IIFE uses as a comma separated list on a var statement at the top of the IIFE immediately after the ‘use strict’; command.
The only use for globals in JavaScript now is the single one for passing a library.
ok had not run into the term IIFE. I already got that then.
I tried to declare the global variable for the coordinates, i tried filling it in the function. But evidently not doing it right.
You haven’t shown the code where you’re trying to pass your variables to other functions. Without seeing that, the best I can guess is that it’s a timing issue. $.get is an asynchronous operation. The global variable will remain blank for an unknown amount of time until the response comes in.
I think I wasn’t clear. Sorry. The code I have, works fine.
The particular snipped i added just provides data such as city, zip, co-ordinates.
I am trying to understand how I can pull out from the function/call the co-ordinates or zip so I can use it in a global variable and pass it on to other functions.
Here is the code however.
$(document).ready(function(){
"use strict";
var coords = "";
$.get("http://ipinfo.io", function (response) {
//coords == response.loc;
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region + ", " + response.loc);
$("#details").html(JSON.stringify(response, null, 3));
$("#geoLoc").css({
"background-color":"#066",
"color":"#fff",
"font-family":"sans-serif",
"font-size":"14px",
"text-transform":"uppercase" });
console.log(response.loc);
}, "jsonp");
// politician feed
$("#rep-lookup").submit(function(e){
e.preventDefault();
var $results = $("#rep-lookup-results");
var zipcode = $("#txt-zip").val();
var apiKey = "######";
var requestURL = "https://congress.api.sunlightfoundation.com/legislators/locate?callback=?";
//collect data
$.getJSON(requestURL,{
'apikey' : apiKey,
'zip' : zipcode
},function(data){
if(data.results && data.results.length > 0){
var senators = "<p> Here you go: </p>";
$.each(data.results, function(i, rep){
if('senate'=== rep.chamber.toLowerCase()){
senators += '<p>';
senators += '<a href="' +rep.contact_form+ '" target="_blank">';
senators += rep.first_name +' '+ rep.last_name;
senators += '</a>';
senators += '</p>';
}
});
senators += '<p> Write em</p>';
$results.html(senators);
$('#subSect0').append($results.html(senators));
}else{
$results.html('<p> none found for' +zipcode+ '</p>');
}
});
//$('#subSect0').append($results.html(senators));
});
// auto pull of committees
$.get("https://congress.api.sunlightfoundation.com/committees?apikey=######", function (response) {
//console.log(response);
var $resultsC = $("#ct2");
var coms = "<p> The committees </p>";
if(response.results && response.results.length > 0){
$.each(response.results, function(i, rep){
if('senate'=== rep.chamber.toLowerCase()){
coms += "<div class=\"bill\">"+ rep.committee_id +"<br/>";
coms += rep.name+"<br/>";
coms += rep.parent_committee_id+"<br/>";
coms += rep.subcommittee+"<br/>";
coms += "</div>";
}
});
coms += '<p> Write em</p>';
$resultsC.html(coms);
}else{
$resultsC.html('<p> none found, no coms for you</p>');
}
}, "jsonp");
// get with for loop
$.get("https://congress.api.sunlightfoundation.com/bills?apikey=########", function (response) {
//console.log(response);
var i=0;
var html = '';
for(i=0; i<response.results.length; i++){
console.log(objList);
var objList = response.results[i];
html += '<div class=\" billList\">'
html += objList.bill_id + '<br />';
html += objList.chamber + '<br />';
html += objList.official_title + '<br />';
html += '</div>'
$("#ct0").append(html);
}
}, "jsonp");
}); // end of document.ready block
Hmm. i suppose also that rather than making the results from the iipinfo global I could pull the requests from sunlight into that same function and pass along the data.
What i wanted to try and do was to grab the zip code from ipiinfo and feed it as part of a request to sunlight. or whatever other api uses co-ordinates so it could pull in that data.
so for example if a user comes upon the site the co-ordinates are picked up. then the other api delivers information that have more meaning for those co-ordinatets.