PDA

View Full Version : GEOIP JavaScript not working correctly



KDNET
05-04-2014, 04:22 PM
Hi, I hope I can get some hints from the pro's in this forum. I am not very familiar with coding and JavaScript and it is difficult to find one in our country in Africa with kind of sophisticaed knowledge. Maybe one of you has a hint on the below.

This is for a website that should re-direct visitors based on their GeoIP to a landing page.
If the visitor is from Namibia (NA) to the NAMIBIA landing page
If the visitor is from a SADC Country (15 countries) to the SADC landing page (in the below sample only NA and ZA is mentioned)
If the visitor is from Germany (DE) to the german landing page.
All others to the default homepage.

The first snippet below should only redirect NA, ZA and DE and for NA works fine. So I assume it also works fine for ZA and DE which I could not test yet.

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">
var country= geoip_country_code();
if(country == "NA")
{
<!--
window.location = "http://www.mokutietoshalodge.com/namibia/specials"
//-->
}
if(country == “ZA")
{
<!--
window.location = "http://www.mokutietoshalodge.com/sadc/specials"
//-->
}
if(country == “DE”)
{
<!--
window.location = "http://de.mokutietoshalodge.com”
//—>
}
else
{
<!--
window.location = "http://www.mokutietoshalodge.com"
//-->
}
</script>

This works actually fine for NA.

Now I would like to differentiate between NA, ZA, DE and the other countries and did the following:

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">
var country= geoip_country_code();
if(country == "NA")
{
<!--
window.location = "http://www.mokutietoshalodge.com/namibia/specials"
//-->
}
else if(country == “ZA")
{
<!--
window.location = "http://www.mokutietoshalodge.com/sadc/specials"
//-->
}
else if(country == “DE”)
{
<!--
window.location = "http://de.mokutietoshalodge.com”
//—>
}

---------
other countries like

}
else if(country == “XX") XX = placeholder for country
{
<!--
window.location = "http://www.mokutietoshalodge.com/landing/page" (placeholder)
//-->
}---------


else
{
<!--
window.location = "http://www.mokutietoshalodge.com"
//-->
}
</script>

Does anyone have a hint for me?
Thank you very much.

xelawho
05-04-2014, 05:28 PM
Its unclear (to me at least) what the placeholder should be here. Can you elaborate?

Philip M
05-04-2014, 05:47 PM
else if(country == “ZA")
else if(country == “DE”)
window.location = "http://de.mokutietoshalodge.com”

You are using literary quotes as per MSWORD and other word processors.

In Javascript only ordinary quotes " may be used. When writing code use only a plain text editor.
e.g.
else if(country == "ZA")

<script language = "Javascript"> is long obsolete. Use <script type = "text/javascript"> instead.

<!-- and //--> are HTML comment tags and invalid in Javascript.

It is recommended that you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage.

All the misery this world contains comes from seeking pleasure for oneself; all the joy this world contains comes from seeking happiniess for others. - Buddist aphorism

KDNET
05-04-2014, 09:22 PM
Its unclear (to me at least) what the placeholder should be here. Can you elaborate?

This will be the other country codes like BW, AN etc. for the country code and the respective URL of the website to wich the redirection needs to go.
Thank You

xelawho
05-04-2014, 10:47 PM
you mean like


if (country=="BW"||country=="AN"||country=="AU"){
window.location = "http://www.mokutietoshalodge.com/landing/page"
}

or am I still not getting it?

Arbitrator
05-04-2014, 11:55 PM
Does anyone have a hint for me?The script at http://j.maxmind.com/app/geoip.js is returning HTTP code 500, indicating a server error, so it's not clear how to accurately troubleshoot your problem. The following code will probably work though. It includes fallback that allows the user to navigate to a specific page if that JavaScript file doesn't load or takes a long time to load.


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML Demo</title>
<style>
dt, dd { display: inline; margin: 0; }
dt { font-weight: bolder; }
dt:not(.country-group)::after { content: ":"; }
dt.country-group + dt::before { content: "("; }
dt.country-group + dt::after { content: "):"; }
dd::after { content: "\a"; white-space: pre; }
</style>
</head>
<body>
<p>You are being redirected to a country‐specific version of our site at <a href="http://www.mokutietoshalodge.com/">http://www.mokutietoshalodge.com/</a>. If you are not redirected, please click the aforementioned link or one of the country‐specific links below:</p>
<dl>
<dt>Germany</dt>
<dd><a href="http://de.mokutietoshalodge.com/">http://de.mokutietoshalodge.com/</a></dd>
<dt>Namibia</dt>
<dd><a href="http://www.mokutietoshalodge.com/namibia/specials">http://www.mokutietoshalodge.com/namibia/specials</a></dd>
<dt class="country-group">South African Development Community</dt>
<dt>South Africa et al</dt>
<dd><a href="http://www.mokutietoshalodge.com/sadc/specials">http://www.mokutietoshalodge.com/sadc/specials</a></dd>
</dl>
<script src="http://j.maxmind.com/app/geoip.js"></script>
<script>
// This script was validated at http://jshint.com/ using the following settings:
/* jshint browser: true, curly: true, eqeqeq: true, devel: false, forin: true, immed: true, latedef: true, noarg: true, noempty: true, nonew: true, plusplus: true, quotmark: double, undef: true, unused: strict, strict: true, trailing: true */
/* global geoip_country_code */
(function () {
"use strict";
if (typeof geoip_country_code === "function") {
var countryCode = geoip_country_code();
var countryURLs = Object.create(null);
Object.defineProperty(countryURLs, "DE", "http://de.mokutietoshalodge.com/");
Object.defineProperty(countryURLs, "NA", "http://www.mokutietoshalodge.com/namibia/specials");
Object.defineProperty(countryURLs, "ZA", "http://www.mokutietoshalodge.com/sadc/specials");
if (typeof countryURLs[countryCode] === "string") {
location.replace(countryURLs[countryCode]);
}
}
location.replace("http://www.mokutietoshalodge.com/");
})();
</script>
</body>
</html>

KDNET
05-06-2014, 06:32 PM
you mean like


if (country=="BW"||country=="AN"||country=="AU"){
window.location = "http://www.mokutietoshalodge.com/landing/page"
}

or am I still not getting it?


Thank you. I take the the || is an OR, correct?
If that is the case, you are 100% right.

KDNET
05-07-2014, 10:26 PM
Thank you very much to all for assistance. We finally got it to work with your hints.