亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

怎么設置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 關注
  • 524 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號