Answer the question
In order to leave comments, you need to log in
How to define a variable and redirect according to the variable?
Good afternoon!
I have a problem that I would like to solve with your help.
Using the Yandex Maps API, I determine the user's location, later I need to redirect him to another site if his city is Moscow or St. Petersburg.
There is this code, but with my knowledge I could not think of anything better.
<!DOCTYPE html>
<html lang="ru">
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="https://api-maps.yandex.ru/2.0/?load=package.full&lang=ru-RU" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
ymaps.ready(function(){
var geolocation = ymaps.geolocation;
$('#cou').html('Ваша страна: '+geolocation.country);
$('#tow').html('Ваш город: '+geolocation.city);
$('#reg').html('Ваш регион: '+geolocation.region)
if ('#tow') == ('Москва') {
location="https://yandex.ru";
} else if ('#tow') == ('Санкт-Петербург'){
location="https://google.com";
} else {
location="https://mail.ru";
}
});
});
</script>
</head>
<body>
<div id="tow">Ваш город: ... определяется ...</div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Replace
with Ps I'm writing from my phone
. So I think you understand the logic
if ('#tow') == ('Москва')
if ($("#tow").html().includes("Москва"))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question