Created
February 24, 2012 13:50
-
-
Save maxcal/1901053 to your computer and use it in GitHub Desktop.
IP2C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(){ | |
| // Always Declare vars at top of function - good practice | |
| var url, countries, defaultKey; | |
| // Get data from here. | |
| url = "http://www.example.com/ajax.do"; | |
| // Use this key per default (international) | |
| defaultKey = "int"; | |
| // Object containing the countries. | |
| countries = { | |
| 'za' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Africa (EN)" | |
| }, | |
| // Australia | |
| 'au' : { | |
| 'url' : "https://www.greenpeace.org.au/action/index.php?cid=22", | |
| 'name' : "Australia Pacific" | |
| }, | |
| // Canada | |
| 'ca' : { | |
| 'url' : "http://greenpeace.ca/savethearctic", | |
| 'name' : "Canada" | |
| }, | |
| // India | |
| 'in' : { | |
| 'url' : "http://www.greenpeace.org/india/en/Get-Involved/Dont-destroy-the-Arctic/", | |
| 'name' : "India" | |
| }, | |
| // PNG | |
| 'gp' : { | |
| 'url' : "http://www.greenpeace.org/australia/savethearctic", | |
| 'name' : "Papua New Guinea" | |
| }, | |
| // UK | |
| 'uk' : { | |
| 'url' : "http://act.greenpeace.org.uk/ea-action/action?ea.client.id=18&ea.campaign.id=13413", | |
| 'name' : "United Kingdom" | |
| }, | |
| // UK (Great Britain bug) | |
| 'gb' : { | |
| 'url' : "http://act.greenpeace.org.uk/ea-action/action?ea.client.id=18&ea.campaign.id=13413", | |
| 'name' : "United Kingdom" | |
| }, | |
| // USA | |
| 'us' : { | |
| 'url' : "https://secure3.convio.net/gpeace/site/Advocacy?cmd=display&page=UserAction&id=1011&s_src=GPI", | |
| 'name' : "United States" | |
| }, | |
| // New Zealand | |
| 'nz' : { | |
| 'url' : "http://www.greenpeace.org/new-zealand/en/take-action/Take-action-online/Save-the-Arctic/", | |
| 'name' : "New Zealand" | |
| }, | |
| // Africa-FR | |
| // @@ TODO: Add country codes for all French-speaking African countries | |
| 'sn' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Africa (FR)" | |
| }, | |
| // Argentina | |
| 'ar' : { | |
| 'url' : "http://www.greenpeace.org.ar/cyberacciones/index.php?cyberaccion=5122&referer=web_gpi", | |
| 'name' : "Argentina" | |
| }, | |
| // Austria | |
| 'at' : { | |
| 'url' : "http://www.greenpeace.org/austria/de/aktivwerden/Proteste/Petition/", | |
| 'name' : "Austria" | |
| }, | |
| // Belgium | |
| 'be' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Belgium" | |
| }, | |
| // Brasil | |
| 'br' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Brazil" | |
| }, | |
| // Chile | |
| 'cl' : { | |
| 'url' : "http://www.greenpeace.org/chile/es/que-hacemos/clima-y-energia/Salva-el-Artico/ ", | |
| 'name' : "Chile" | |
| }, | |
| // China | |
| 'cn' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "China (EN)" | |
| }, | |
| // Colombia | |
| 'co' : { | |
| 'url' : "http://www.greenpeace.org/colombia/es/participa/ciberacciones/Salva-el-artico/", | |
| 'name' : "Colombia" | |
| }, | |
| // Czech | |
| 'cz' : { | |
| 'url' : "http://www.greenpeace.org/czech/cz/Kampan/klima_a_energetika/Arktida/zachrante-arktidu/poslete-email/formular/", | |
| 'name' : "Czech Republic" | |
| }, | |
| // Denmark | |
| 'dk' : { | |
| 'url' : "http://www.greenpeace.org/denmark/da/Hvad-du-kan-gore/beskyt-Arktis/", | |
| 'name' : "Denmark" | |
| }, | |
| // Finland | |
| 'fi' : { | |
| 'url' : "http://www.greenpeace.org/finland/fi/kampanjat/ilmastonmuutos/Arktis/savethearctic/", | |
| 'name' : "Finland" | |
| }, | |
| // France | |
| 'fr' : { | |
| 'url' : "http://www.greenpeace.fr/arctique-en-danger/", | |
| 'name' : "France" | |
| }, | |
| // Germany | |
| 'de' : { | |
| 'url' : "http://service.greenpeace.de/themen/oel/mitmach_aktionen/aktion/protest_e_mail/rettet_die_arktis/", | |
| 'name' : "Germany" | |
| }, | |
| // Greece | |
| 'gr' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Greece" | |
| }, | |
| // Hungary | |
| 'hu' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : " Hungary" | |
| }, | |
| // Indonesia | |
| 'id' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Indonesia" | |
| }, | |
| // Israel | |
| 'il' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Israel" | |
| }, | |
| // Italy | |
| 'it' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Italy" | |
| }, | |
| // Japan | |
| 'jp' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Japan" | |
| }, | |
| // Lebanon | |
| 'lb' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Lebanon" | |
| }, | |
| // Luxembourg | |
| 'lu' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Luxembourg" | |
| }, | |
| // Mexico | |
| 'mx' : { | |
| 'url' : "http://www.greenpeace.org/mexico/es/Ciberacciones/Salva-el-Artico/", | |
| 'name' : "Mexico" | |
| }, | |
| // Netherlands | |
| 'nl' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Netherlands" | |
| }, | |
| // Norway | |
| 'no' : { | |
| 'url' : "http://www.greenpeace.org/norway/no/getinvolved/Online-actions/beskytt-arktis/", | |
| 'name' : "Norway" | |
| }, | |
| // Philippines | |
| 'ph' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Philippines" | |
| }, | |
| // Poland | |
| 'pl' : { | |
| 'url' : "http://www.greenpeace.org/poland/pl/dzialaj-teraz/ratuj-arktyke/", | |
| 'name' : "Poland" | |
| }, | |
| // Russia | |
| 'ru' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Russia" | |
| }, | |
| // Slovakia | |
| 'sk' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Slovakia" | |
| }, | |
| // Solomon Islands | |
| 'sb' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Solomon Islands" | |
| }, | |
| // Spain | |
| 'es' : { | |
| 'url' : "http://www.greenpeace.org/espana/es/Que-puedes-hacer-tu/Ser-ciberactivista/salvemos-el-artico/", | |
| 'name' : "Spain" | |
| }, | |
| // Sweden | |
| 'sw' : { | |
| 'url' : "http://www.greenpeace.org/sweden/se/agera/Agera-online/skydda-arktis/ ", | |
| 'name' : "Sweden" | |
| }, | |
| // Switzerland | |
| 'ch' : { | |
| 'url' : "http://www.greenpeace.org/switzerland/de/Kampagnen/Stromzukunft-Schweiz/Klima/arctic-impacts/arctic/", | |
| 'name' : "Switzerland (DE)" | |
| }, | |
| // Thailand | |
| 'th' : { | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "Thailand" | |
| }, | |
| // Turkey | |
| 'tr' : { | |
| 'url' : "http://www.greenpeace.org/turkey/tr/harekete-gec/kuzey-kutbunu-kurtar/", | |
| 'name' : "Turkey" | |
| }, | |
| // Bolivia | |
| 'bo' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Bolivia" | |
| }, | |
| // Belize | |
| 'bz' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Belize" | |
| }, | |
| // Costa Rica | |
| 'cr' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Costa Rica" | |
| }, | |
| // Cuba | |
| 'cu' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Cuba" | |
| }, | |
| // Dominican Republic | |
| 'do' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Dominican Republic" | |
| }, | |
| // Ecuador | |
| 'ec' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Ecuador" | |
| }, | |
| // Guatemala | |
| 'gt' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Guatemala" | |
| }, | |
| // Nicaragua | |
| 'ni' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Nicaragua" | |
| }, | |
| // Panama | |
| 'pa' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Panama" | |
| }, | |
| // Peru | |
| 'pe' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Peru" | |
| }, | |
| // Puerto Rico | |
| 'pr' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Puerto Rico" | |
| }, | |
| // Paraguay | |
| 'py' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Ecuador" | |
| }, | |
| // El Salvador | |
| 'sv' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "El Salvador" | |
| }, | |
| // Uruguay | |
| 'uy' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Uruguay" | |
| }, | |
| // Venezuela | |
| 've' : { | |
| 'url' : "http://www.salvaelartico.org", | |
| 'name' : "Venezuela" | |
| }, | |
| 'int': { | |
| // The rest go to GPI | |
| 'url' : "http://www.greenpeace.org/international/en/campaigns/climate-change/arctic-impacts/Save-the-Arctic/?redirect", | |
| 'name' : "International" | |
| } | |
| }; | |
| /** | |
| * Get the correct key out a object or if not found returns defualt option | |
| * @param obj object | |
| * @param key string | |
| * @param defaultKey string | |
| */ | |
| function getHashKey(obj, key, defaultKey){ | |
| // We check if the key actually exists | |
| // Second condition also checks type | |
| return ((obj.hasOwnProperty(key)) && (typeof obj[key].url === 'string')) | |
| ? obj[key].url : obj[defaultKey].url; | |
| } | |
| /** | |
| * get an IP2country key. | |
| */ | |
| $.getJSON(url, function(data){ | |
| var redirectUrl = getHashKey(countries, data.key, defaultKey); | |
| if ( redirectUrl !== window.location ) { | |
| window.location = data.url; | |
| } | |
| }); | |
| }()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @see: http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/ | |
| **/ | |
| // Do logic to determine user location | |
| // Turn data into an assoc array | |
| $data = array( | |
| "id" => "es", | |
| "url" => "http://www.example.com" | |
| ); | |
| header('content-type: application/json; charset=utf-8'); | |
| $json = json_encode($data); | |
| echo isset($_GET['callback']) | |
| ? "{$_GET['callback']}($json)" | |
| : $json; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment