的window.open()的作品不同的AJAX成功 [英] window.open() works different on AJAX success

查看:272
本文介绍了的window.open()的作品不同的AJAX成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/9793774/window-openurl-different-behavior-same-$c$c-different-timing">window.open(url)不同的行为 - 同code,不同的时间

这将是更容易让我来解释这个问题,如果我只是告诉你,例如 - > http://jsfiddle.net/RU2SM/
正如你可以看到有2个按钮,一个called'AJAX和一个叫'直接'......所以,如果你点击'直接'它打开的窗口(在Chrome新标签页),但如果我尝试做的window.open( )对AJAX的成功处理,这是行不通的方式相同。
我敢肯定,这个问题是来自AJAX,但我不知道如何解决它。
请问AP preciate什么好的想法。 谢谢

It will be more easy for me to explain the problem if I just show you that example -> http://jsfiddle.net/RU2SM/
As you can see there are 2 buttons, one called'AJAX' and one called 'Direct'... So if you click on 'Direct' it opens window (new tab on Chrome) but if I try to make window.open() on AJAX success handler, it doesn't work same way.
I'm sure that the problem is from AJAX but I have no idea how to fix it.
Will appreciate any good ideas. Thanks

推荐答案

这就像一个魅力:

// Direct window.open()
$('#btnDirect').on('click',function(){
    window.open('http://google.com')
})
var success = false;  //NOTE THIS

// AJAX window.open()
$('#btnAJAX').on("click", function(){
    $.ajax({
      url: "/user/login/",
      context: document.body,
      async:false,   //NOTE THIS
      success: function(){  //THIS ALSO CHANGED
         success = true
      }
    });
    if(success){ //AND THIS CHANGED
      window.open('http://google.com')
    }
})

在Ajax调用是成功它设置变量成功真的这样做是什么。
异步:假 propperty确保了if语句触发Ajax调用完成后
。 所以的window.open触发不变的情况下为您的直接联系。

What this does is when the Ajax call is success it sets the variable success to true.
The async:false propperty makes sure that the if statement is fired after the Ajax call is completed.
So the window.open is fired in the same circumstances as your direct link.

这篇关于的window.open()的作品不同的AJAX成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆