S
S
Satir012015-01-25 15:48:44
PHP
Satir01, 2015-01-25 15:48:44

Why is the javascript redirect not working?

Good afternoon!
The feedback form is called using inline fancybox.
In an inline element

<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>

there is a script
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;
          
        }

Why is it not executed?
setTimeout("document.location.href='/'", 1500);

UPD Unfortunately, the three proposed options do not want to be executed in fancybox inline.
Changed inline to iframe, but redirect opens landing page in ifreme. I will sort this out.
UPD 2 Didn't have to figure it out Vitaly Vitaly suggested a solution for inline. Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Vitaly, 2015-01-25
@Satir01

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;

            }
        };

D
Deodatuss, 2015-01-25
@Deodatuss

what if it's like this?

setTimeout(function(){window.location.href='/'}, 1500);

E
Eugene D., 2015-01-25
@devg

Try like this:

setTimeout(function(){ document.location.href='/'; }, 1500);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question