Some businesses don’t need to “block access” — they need a lighter form of “geo routing”: on the same page, users from different countries/regions should be sent to different target URLs when they click a link or a button. This kind of routing is commonly used for: assigning download URLs, region-specific payment entry points, different customer support systems, and switching entry points between sites (global/local), etc.
This article focuses on one core idea: when the page loads, the frontend sends GET https://my.ipin.io/info to get the visitor’s country code country,
then sets the href of an <a> or the click redirect URL of a <button> to the URL for that region.
For example, the API may return:
{"ip":"185.220.236.7","country":"TW","region":"Taiwan","city":"Taipei"}
1) Logic and how it works (2 paragraphs)
Paragraph 1: Flow
Page loads → the frontend immediately requests https://my.ipin.io/info → gets country → chooses a target URL based on your rules (e.g., TW uses A URL #1, US uses A URL #2, other regions use a default URL) →
updates the “destination” of the link or button to that target URL. Only when the user clicks will they go to the corresponding address.
Paragraph 2: Why it’s recommended to set a default URL first, then override by region
Because the frontend environment is not always stable: network issues, CORS, ad blockers, and API timeouts can all prevent you from getting country. A safer approach is:
put a default usable URL on the page (a generic landing page), then override it with a region-specific URL after the script successfully determines the region. This way, even if the API fails, the page can still redirect normally, and you won’t run into “nothing happens when clicking / empty URL” issues.
2) Code implementation (super simple, A and button explained separately)
2.1 A link (using id): set href based on country
Scenario: there is only one key link on the page, and you want it to point to different destinations based on region. The example below demonstrates the simplest logic:
if it’s TW, use the TW link; otherwise use the default link.
<a id="geoLink" href="https://example.com/default">Go now</a>
<script>
(async function () {
try {
const info = await fetch("https://my.ipin.io/info").then(r => r.json());
const target = (info.country === "TW")
? "https://example.com/tw"
: "https://example.com/default";
const a = document.getElementById("geoLink");
if (a) a.href = target;
} catch (e) {
// On failure, keep the default href
}
})();
</script>
2.2 A links (using class): batch set href based on country
Scenario: multiple links on the page need geo routing. The simplest approach is: give them the same class and update them together. The example remains minimal: TW uses the TW link; other regions use the default link.
<a class="geo-link" href="https://example.com/default">Download</a>
<a class="geo-link" href="https://example.com/default">Purchase</a>
<script>
(async function () {
try {
const info = await fetch("https://my.ipin.io/info").then(r => r.json());
const target = (info.country === "TW")
? "https://example.com/tw"
: "https://example.com/default";
document.querySelectorAll(".geo-link").forEach(function (el) {
el.href = target;
});
} catch (e) {}
})();
</script>
2.3 Button (using id): redirect based on country after click
Scenario: a button is not a link itself, so you need to bind a click handler on the frontend. This example keeps only the essential logic: first choose a target URL based on country, then redirect to that URL when the button is clicked.
<button id="geoBtn" type="button">Click to redirect</button>
<script>
(async function () {
let target = "https://example.com/default"; // Default URL
try {
const info = await fetch("https://my.ipin.io/info").then(r => r.json());
if (info.country === "TW") target = "https://example.com/tw";
} catch (e) {
// On failure, use the default URL
}
const btn = document.getElementById("geoBtn");
if (btn) btn.onclick = function () { window.location.href = target; };
})();
</script>
2.4 Buttons (using class): batch bind click redirects
Scenario: multiple buttons on the page need region-based redirects. The simplest approach: use one class and bind click handlers in a loop. Note: buttons don’t have a “default href”, so it’s recommended to prepare a default URL in the script to avoid “no response” when the API fails.
<button class="geo-btn" type="button">Button 1</button>
<button class="geo-btn" type="button">Button 2</button>
<script>
(async function () {
let target = "https://example.com/default"; // Default URL
try {
const info = await fetch("https://my.ipin.io/info").then(r => r.json());
if (info.country === "TW") target = "https://example.com/tw";
} catch (e) {}
document.querySelectorAll(".geo-btn").forEach(function (btn) {
btn.onclick = function () { window.location.href = target; };
});
})();
</script>
2.5 Multiple regions (TW, US…): “Set the target URL directly” with a mapping table (shortest usable)
The simplest rule: prepare a default URL first, then override it with a mapping table map based on the country code.
If there’s a mapping, use it; if not, use the default. The key is: after you compute target, immediately apply it to an A link’s href or a button’s click handler.
2.5.1 For an A link (id): set href directly
<a id="geoLink" href="https://example.com/default">Go now</a>
<script>
(async function () {
const defaultUrl = "https://example.com/default";
const map = { TW: "https://example.com/tw", US: "https://example.com/us" };
try {
const info = await fetch("https://my.ipin.io/info").then(r => r.json());
const target = map[info.country] || defaultUrl;
document.getElementById("geoLink").href = target; // ✅ Key: write target into href
} catch (e) {
// If it fails, keep the default href
}
})();
</script>
2.5.2 For a button (id): navigate to target on click
<button id="geoBtn" type="button">Click to go</button>
<script>
(async function () {
const defaultUrl = "https://example.com/default";
const map = { TW: "https://example.com/tw", US: "https://example.com/us" };
let target = defaultUrl;
try {
const info = await fetch("https://my.ipin.io/info").then(r => r.json());
target = map[info.country] || defaultUrl;
} catch (e) {}
document.getElementById("geoBtn").onclick = function () {
window.location.href = target; // ✅ Key: navigate using target on click
};
})();
</script>
3) Implementing in WordPress / Joomla / Magento / Shopify (frontend approach + code)
For “region-based A link / button destination changes”, the essence is placing the script into the site-wide Head or layout file so it runs as early as possible. You don’t need to touch backend logic — as long as the script runs on page load, you can prepare the destination before the user clicks.
3.1 WordPress
- Use a “insert header/footer scripts” plugin and paste the script into the Header (site-wide)
- Or insert the script before
</head>in the theme fileheader.php
3.2 Joomla! / Joomla
- In the current template’s
index.php, find<head>and paste the script before</head> - Or use the template’s “custom head code” option (varies by template)
3.3 Magento
- Add the script to the theme’s global head (the head area in theme/layout)
- The goal is site-wide output so you can maintain region mapping rules in one place
3.4 Shopify
- Online Store → Themes → Edit code
- Find
theme.liquidand paste the script before</head>(site-wide)
3.5 General rule
- Any system that allows editing the global
<head>or layout file can implement “set link/button destinations by IP region” the same way - Keep a default URL so the page remains usable when the external API fails
4) Summary
> When the page loads, request https://my.ipin.io/info to get country, then choose a target URL based on country and update either the A link href or the button click redirect URL, achieving a frontend solution for “routing to different destinations by IP region”.
In practice, the two most important points are: first, place the script as early as possible in the head so the destination is prepared early; second, always provide a default URL to prevent invalid links or unresponsive buttons when the API fails. As long as you maintain your region mapping rules properly, this approach works reliably for download entry points, payment entry points, and support entry points.
5) FAQ
console.log(info) to confirm that you actually received country.
window.location.href = target with window.open(target, "_blank"). It usually won’t be blocked by browsers when triggered inside a click event.
{ TW: "...", US: "..." }. If there’s a match, use that URL; otherwise fall back to a default URL.
https://my.ipin.io/info due to CORS, it can’t get country, and it will ultimately fall back to the default URL. Before going live, it’s recommended to verify in the console that the request succeeds.