javascript - How to show or hide element based on an Array of Geo ip -
hi looking if statement in js, want "hello europe" in html if visiting ip europe.
css
#us { text-align: left; color: blue; display:none;} #ca { text-align: left; color: blue; display:none;} #world { text-align: left; color: blue; display:none;} #europe { text-align: left; color: blue; display:none;}
html
<div id="ip">loading...</div> <div id="country_code"></div> <div id="ca">hello canada</div> <div id="us">hello usa</div> <div id="world">hello world</div> <div id="europe">hello europe</div>
js
$.get("http://freegeoip.net/json/", function (response) { $("#ip").html("ip: " + response.ip); $("#country_code").html(response.country_code); var country = response.country_code; var europe = ['al','ad','at','by','be','ba','bg','hr','cy','cz','dk','ee','fo','fi','fr','de','gi','gr','hu','is','ie','it','lv','li','lt','lu','mk','mt','md','mc','nl','no','pl','pt','ro','ru','sm','rs','sk','si','es','se','ch','ua','va','rs','im','rs','me']; if(country == 'us' || country == 'ca') document.getelementbyid(country).style.display = "block"; else if (country === 'europe') document.getelementbyid('europe').style.display = "block"; else document.getelementbyid('world').style.display = "block"; }, "jsonp");
a simple indexof
should nicely here.
$.get("http://freegeoip.net/json/", function(response) { $("#ip").html("ip: " + response.ip); $("#country_code").html(response.country_code); var country = response.country_code; var europe = ['al', 'ad', 'at', 'by', 'be', 'ba', 'bg', 'hr', 'cy', 'cz', 'dk', 'ee', 'fo', 'fi', 'fr', 'de', 'gi', 'gr', 'hu', 'is', 'ie', 'it', 'lv', 'li', 'lt', 'lu', 'mk', 'mt', 'md', 'mc', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'sm', 'rs', 'sk', 'si', 'es', 'se', 'ch', 'ua', 'va', 'rs', 'im', 'rs', 'me']; var ineu = europe.indexof(country) !== -1; if (country == 'us' || country == 'ca') document.getelementbyid(country).style.display = "block"; else if (ineu) document.getelementbyid('europe').style.display = "block"; else document.getelementbyid('world').style.display = "block"; }, "jsonp");
link docs further reading: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/indexof
Comments
Post a Comment