为了账号安全,请及时绑定邮箱和手机立即绑定

怎么设置JQuery事件时绕过窗口上的弹出阻止程序。

怎么设置JQuery事件时绕过窗口上的弹出阻止程序。

慕斯王 2019-10-20 16:12:27
设置JQuery事件时绕过窗口上的弹出阻止程序。我想在超链接的单击事件上有条件地显示一个JQuery对话框。我有一个类似于onCondition1的要求,打开一个JQuery对话,如果条件1不满足,导航到由其单击事件的‘href’标记引用的页面。我能够在链接的点击事件上调用一个函数。这个函数现在通过执行另一个URL(执行我的Spring控制器并返回响应)来检查上述条件。所有操作都很完美,只有窗口。打开被弹出窗口阻止。$('a[href*=/viewpage?number]').live('click', function(e) {     e.preventDefault();     redirectionURL = this.href;     pageId= getUrlVars(redirectionURL)["number"];     $.getJSON("redirect/" + pageId, {}, function(status) {         if (status == null) {             alert("Error in verifying the status.");         } else if(!status) {             $("#agreement").dialog("open");         } else {             window.open(redirectionURL);         }     });});如果我把e.preventDefault();从代码中,popoup阻止程序不会阻塞页面,但是对于条件1,它会打开对话并打开‘href’页面。如果我解决了一个问题,就会给另一个人制造问题。我不能同时公正地对待这两种情况。你能帮我解决这个问题吗?一旦这个问题解决了,我还有另一个问题要解决,那就是在对话的OK事件上导航:)
查看完整描述

3 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

弹出阻止程序通常只允许window.open如果使用期间用户事件的处理(如单击)。在你的情况下,你打电话给window.open 后来,而不是在活动期间,因为$.getJSON是异步的。

你有两个选择:

  1. 做点别的事,而不是window.open.

  2. 让Ajax调用是同步的,这是您通常应该避免的事情,因为它锁定了浏览器的UI。$.getJSON相当于:

    $.ajax({
      url: url,
      dataType: 'json',
      data: data,
      success: callback});

    .这样你就可以$.getJSON通过将Params映射到上面并添加async: false:

    $.ajax({
        url:      "redirect/" + pageId,
        async:    false,
        dataType: "json",
        data:     {},
        success:  function(status) {
            if (status == null) {
                alert("Error in verifying the status.");
            } else if(!status) {
                $("#agreement").dialog("open");
            } else {
                window.open(redirectionURL);
            }
        }});

    同样,如果您能够找到实现目标的其他方法,我也不提倡同步Ajax调用。但如果你做不到,那就去吧。

    下面是由于异步调用导致测试失败的代码示例:

(由于对JSBin的更改,活动链接不再工作)

jQuery(function($) {
  // This version doesn't work, because the window.open is
  // not during the event processing
  $("#theButton").click(function(e) {
    e.preventDefault();
    $.getJSON("http://jsbin.com/uriyip", function() {
      window.open("http://jsbin.com/ubiqev");
    });
  });});

下面是一个实际工作的例子,使用同步调用:

(由于对JSBin的更改,活动链接不再工作)

jQuery(function($) {
  // This version does work, because the window.open is
  // during the event processing. But it uses a synchronous
  // ajax call, locking up the browser UI while the call is
  // in progress.
  $("#theButton").click(function(e) {
    e.preventDefault();
    $.ajax({
      url:      "http://jsbin.com/uriyip",
      async:    false,
      dataType: "json",
      success:  function() {
        window.open("http://jsbin.com/ubiqev");
      }
    });
  });});



查看完整回答
反对 回复 2019-10-21
?
12345678_0001

TA贡献1802条经验 获得超5个赞

我有这个问题,我还没有准备好我的url,直到回调会返回一些数据。解决方案是在启动回调之前打开空白窗口,然后在回调返回时设置位置。

$scope.testCode = function () {
    var newWin = $window.open('', '_blank');
    service.testCode().then(function (data) {
        $scope.testing = true;
        newWin.location = '/Tests/' + data.url.replace(/["]/g, "");
    });};



查看完整回答
反对 回复 2019-10-21
  • 3 回答
  • 0 关注
  • 474 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信