Сейчас узнаем, какой подарок выпадет именно Вам
Крутите колесо
Как определить город посетителя через Tilda
Discover the Vision
Core foundational elements were defined to guide the project’s development. A flexible structural framework was created to support future expansion.
Project Name
This abstract project focuses on developing adaptable ideas that can function across various contexts.

Как определить город посетителя через Tilda

1
Создали любой блок и в нужном месте в тексте прописали символы трёх точек ... и назначили им ссылку #yourcity
В этом месте и подставится найденный город
2
Вставили код на страницу в блок Т123
Библиотека для примера
<script>
(function() {
  'use strict';

  const CACHE_ENABLED = true;
  const CACHE_HOURS = 24;
  const NOCITY_TEXT = 'Ваш город';
  const GEO_TIMEOUT = 15000;

  function getCachedCity() {
    if (!CACHE_ENABLED) return null;
    try {
      const cached = localStorage.getItem('tildaGeoCity');
      if (!cached) return null;
      const data = JSON.parse(cached);
      const maxAge = CACHE_HOURS * 60 * 60 * 1000;
      if (Date.now() - data.timestamp < maxAge) {
        console.log('[TildaGeo] Город из кеша:', data.city);
        return data.city;
      }
      localStorage.removeItem('tildaGeoCity');
    } catch (e) {
      localStorage.removeItem('tildaGeoCity');
    }
    return null;
  }

  function setCachedCity(city) {
    if (!CACHE_ENABLED || !city) return;
    try {
      localStorage.setItem('tildaGeoCity', JSON.stringify({
        city: city,
        timestamp: Date.now()
      }));
    } catch (e) {}
  }

  function displayCity(city) {
    const text = city || NOCITY_TEXT;
    document.querySelectorAll('a[href="#yourcity"]').forEach(function(el) {
      el.textContent = text;
    });
  }

  function fetchGeo() {
    return new Promise(function(resolve) {
      var controller = new AbortController();
      var timeoutId = setTimeout(function() {
        controller.abort();
      }, GEO_TIMEOUT);

      fetch('https://geo.tildacdn.com/geo/full/', { signal: controller.signal })
        .then(function(response) {
          if (timeoutId) clearTimeout(timeoutId);
          if (!response || !response.ok) return null;
          return response.text();
        })
        .then(function(text) {
          var parsed = text ? JSON.parse(text) : null;
          resolve(parsed);
        })
        .catch(function() {
          resolve(null);
        });
    });
  }

  function extractCity(data) {
    if (!data) return null;
    try {
      var city = data.city && (data.city.name_ru || data.city.name_en);
      if (!city && data.region) {
        city = data.region.name_ru || data.region.name_en;
      }
      return city || null;
    } catch (e) {
      return null;
    }
  }

  function init() {
    var cached = getCachedCity();
    if (cached) displayCity(cached);

    fetchGeo().then(function(data) {
      var city = extractCity(data);
      if (city) {
        setCachedCity(city);
        displayCity(city);
        console.log('[TildaGeo] Город:', city);
      } else if (!cached) {
        displayCity(null);
      }
    }).catch(function(err) {
      console.error('[TildaGeo] Ошибка:', err);
      if (!cached) displayCity(null);
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();
</script>

<style>
  a[href="#yourcity"] {
    pointer-events: none;
  }
</style>
Made on
Tilda