javascript - window.location.href does not jump immediately in the method. Is there any way to solve it?
欧阳克
欧阳克 2017-06-26 10:58:15
0
7
2195

Question: Why is location.href = "../exit.html"; not executed, but window.location.href = 'http://www.baidu. com';?

Is there any way to finish executing getData(). If the data acquisition fails, jump to ../exit.html and no longer execute gourl(); What about the method?

Supplement:async: false in ajax is a synchronous request! ! ! , this is just a simple demo. In fact, there may be a lot of logic behind the getData() method, but if getData() fails to obtain data, the program will not be allowed to execute other methods, and other methods may not be executed at the same time. inside a file.


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
</head>

<body>
    <p>
        <h2>我是测试页面,看我是否发生跳转</h2></p>
    <script src="https://cdn.bootcss.com/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
        getData();
        gourl();
    });

    function getData() {
        var is_success = false;
        $.ajax({
            type: "GET",
            url: "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=bug&bk_length=600",
            data: "",
            dataType: "json",
            async: false,
            success: function(data) {
                if (data.status == 200) {
                    is_success = true;
                } else {
                    is_success = false;
                }
            }
        });
        if (!is_success) {
            location.href = "../exit.html";
        }
    }

    function gourl() {
        console.log('我被执行了');
        window.location.href = 'http://www.baidu.com';
    }
    </script>
</body>

</html>

欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all(7)
女神的闺蜜爱上我

Then you can call back gourl after the success of the getData method to perform the logical processing you want
In addition, I don’t know how to judge your is_success specifically because there is $ajax and a corresponding error

代言

Your code is equivalent to executing the following two sentences:

location.href = '../exit.html';
location.href = 'http://www.baidu.com';

When these two sentences are executed continuously, they will jump to the following address

My guess is that it takes time for the browser to access the first one, but before it succeeds, the second jump comes again, so I give up the first jump and execute the second jump, similar to quickly entering the address twice in the URL. Same.

为情所困

The

gourl() function cannot be called in front, but should be placed in the middle of the Ajax logic, and add else{gourl();}
that is:

after the if logic
if (!is_success) {
        location.href = "../exit.html";
    }else {
        gourl();
    }
学习ing

The code of the questioner can be understood as follows:

    <script type="text/javascript">
    $(function() {
        getData();
    });

    function getData() {
        var is_success = false;
        $.ajax({
            type: "GET",
            url: "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=bug&bk_length=600",
            data: "",
            dataType: "json",
            async: false,
            success: function(data) {
                if (data.status == 200) {
                    is_success = true;
                } else {
                    is_success = false;
                }
            },
            error: function() { ... }
        });
        if (!is_success) {
            location.href = "../exit.html";
        }
        console.log('我被执行了');
        window.location.href = 'http://www.baidu.com';
    }
    </script>

When there are two consecutive location.hrefs in the code, the subsequent jump will be executed. The subject of this question can try it himself.

In addition, since ajax is asynchronous, the subject needs to write if(!is_success) into the error in ajax, or into the else judgment in success, otherwise it will jump regardless of whether ajax is successful or not. gourl() should also be written into success.

In addition, cross-domain errors should occur if ajax is used directly like this. It is recommended to use a proxy or other methods to solve cross-domain problems.

ringa_lee

Mobile phone code, is this what you mean?

$(function() {
    var dtd = $.Deferred();
    dtd
      .done(function(){
          console.log('我被执行了');
          window.location.href = 'http://www.baidu.com';
      })
      .fail(function(){
          console.log('我被抛弃了');
          window.location.href = "../exit.html";
      });
    $.get("http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=bug&bk_length=600", "json")
      .done(function(data) {
          if (data.status == 200) {
              dfd.resolve();
          } else {
              dtd.reject();
          }
      });
});

Mainly using jQ’s promises, all written asynchronously, all successful callbacks of ajax are placed in the done of Deferred (if there are multiple, they can also be written as arrays), and then just give a status directly in the done of ajax.

某草草

Since getData and gourl have an execution relationship, either gourl is placed in the callback judgment. This can be suitable for asynchronous use.
If it’s synchronization with the subject, then it’s okay

    $(function() {
        getData();
        gourl();
    });

Can I control Gourl directly here to execute it?

三叔

There may be something wrong with your code logic. Ajax is asynchronous. gourl(); This function should not be called in that place. It can be called in the success or failure callback of the ajax request.
Looking at your request, it should be called in success.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template