S
S
Shakhrom Mukumov2017-05-14 18:29:59
JavaScript
Shakhrom Mukumov, 2017-05-14 18:29:59

How to link a Geo-redirect to a Cookie?

Good day, help a newbie. I made a georedirect based on the Yandex Maps API, the question is: How to make the redirect work once at the specified time (hour / 24 hours / week / month), etc., otherwise everything works now, but the redirect is permanent.

new ymaps.ready(function() {
  var myCity = ymaps.geolocation.city;
  if (myCity == "Санкт-Петербург") window.location.href = "http://mysite.ru" // если город СПб, то редирект на указанный сайт
});

There is very little information about cookies, and even those that I found, I didn’t figure out how to work with them ((

Thank you in advance for your time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shakhrom Mukumov, 2017-05-16
@leon9208

The issue is resolved, the code may not be perfect, I'm a beginner, so I'll only be happy with comments ))

$(document).ready(function() {
  // Создаем куки
  var is_cookie = document.cookie.match(new RegExp("userredirect=1")),
      // Время с момента посещения сайта + 1 час (первое значение можно менять)
 	    date = new Date(new Date().getTime() + 1*60*60*1000);
  
 		if (!is_cookie) {
      //в ранее созданный куки добавляем значения, в данном случае куки живет 1 час
 			document.cookie = "userredirect=1; path=/; expires=" + date.toString(); 
      //API яндекс карт, именно с помощью него и происходит редирект
 			new ymaps.ready(function() {
        //определение города, можно еще .region-область,регион / .country-страна
 				var myCity = ymaps.geolocation.city;
        //если местоположение соответсвует указанному значению, то редирект на сайт
 				if (myCity == "Санкт-Петербург") window.location.href = "https://vk.com/"
 			});
 		}
 	});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question