@Satir01

Почему не выполняется редирект javascript?

Добрый день!
Форма обратной связи вызывается с помощью inline fancybox'а.
В инлайн элементе
<form class="ask-form" id="ask-form" action='/send/index.php' method='POST' name='form' target='_blank'>
				<div class="ask-title">задайте ваш вопрос</div>
				<label for="name">представьтесь, пожалуйста</label>
				<input id="name" required="" placeholder="" name="name" autocomplete="name"></input>
				<label for="email">укажите email</label>
				<input id="email" type="email" required="" placeholder="" name="email" autocomplete="email"></input>
				<label for="tel">номер телефона</label>
				<input id="tel" type="tel" required="" placeholder="" name="tel" autocomplete="tel"></input> 
				<label for="question">и ваш вопрос</label>
				<textarea id="question" type="text" required="" placeholder="" name="question" autocomplete="question" 	rows="3"></textarea>
				<input type="submit" value="отправить вопрос" class="btn btn-yellow btn-mediun btn-gcenter"></input>		
			</form>

есть скрипт
document.getElementById('ask-form').onsubmit = function(){
				  var http = new XMLHttpRequest();
				  http.open("POST", "/send/index.php", true);
				  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				  http.send("name=" + this.name.value + "&email=" + this.email.value + "&tel=" + this.tel.value + "&question=" + this.question.value);
				  http.onreadystatechange = function() {
					if (http.readyState == 4 && http.status == 200) {
					  alert(http.responseText +', Ваше сообщение отправлено.\nСпасибо!');
					  setTimeout("document.location.href='/'", 1500);
					}
				  }
				  http.onerror = function() {
					alert('Извините, данные не были переданы');
				  }
				  return false;
				  
				}

Почему не выполняется?
setTimeout("document.location.href='/'", 1500);


UPD К сожалению три предложенных варианта не хотят исполняться в инлайне fancybox'a.
Изменил инлайн на iframe, но редирект открывает целевую страницу в ifreme'e. Буду разбираться.

UPD 2 Разбираться не пришлось Vitaly Vitaly подсказал решение для инлайна. Спасибо.
  • Вопрос задан
  • 2690 просмотров
Решения вопроса 1
@V_Tjuryakin
Перфекто
var http = new XMLHttpRequest();
          http.open("POST", "/send/index.php", true);
          http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          http.send("name=" + this.name.value + "&email=" + this.email.value + "&tel=" + this.tel.value + "&question=" + this.question.value);
          http.onreadystatechange = function() {
          if (http.readyState == 4 && http.status == 200) {
            alert(http.responseText +', Ваше сообщение отправлено.\nСпасибо!');
            setTimeout(function() { location.href='/'; }, 1500);
          }
          }
          http.onerror = function() {
          alert('Извините, данные не были переданы');
          }
          return false;
          
        }


UPD:
window.onload = function() {
            document.getElementById('ask-form').onsubmit = function(){
 
                var http = new XMLHttpRequest();
                http.open("POST", "/send/index.php", true);
                http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                http.send("name=" + this.name.value + "&email=" + this.email.value + "&tel=" + this.tel.value + "&question=" + this.question.value);
                http.onreadystatechange = function() {
                    if (http.readyState == 4 && http.status == 200) {
                        alert(http.responseText +', Ваше сообщение отправлено.\nСпасибо!');
                        setTimeout(function() { location.href='/'; }, 1500);
                    }
                }
                http.onerror = function() {
                    alert('Извините, данные не были переданы');
                }

                return false;

            }
        };
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
devg
@devg
I am Yenior software developer
Попробуйте вот так:
setTimeout(function(){ document.location.href='/'; }, 1500);
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы