2016-12-09 43 views
0

我有一個鏈接到Tumblr共享窗口小部件,並且需要在它打開之前附加一些查詢字符串參數(點擊鏈接觸發)。但是,鏈接在添加這些參數之前打開,我認爲這是因爲它需要幾秒鐘 - 我要在頁面上發送圖像以在imgur上託管並返回該URL。只有在執行某些事件後才能在鏈接中打開href

有什麼辦法可以延遲新鏈接被打開,直到我的新圖像url返回後?我試過使用e.preventDefault();return false;,但沒有任何運氣。

我的HTML是:

<button id='tumblrshare'>tumblr</button 


$('body').on('click','#tumblrshare',function(e){ 
var svg = $("#svg")[0]; 
svg.toDataURL("image/png", { 
    callback: function(img) { 
    var img = img.replace("data:image/png;base64,", ""); 
    var imgurTitle = $("meta[property='og:title']").attr("content"); 
    $.ajax({ 
      url: 'https://api.imgur.com/3/image', 
      headers: {'Authorization': 'Client-ID **********'}, 
      type: 'POST', 
      data: {'image': img, 'type': 'base64', 'title': imgurTitle}, 
      success: function(result) { 
      imageURL = result.data.link; 
      window.location = 'https://www.tumblr.com/widgets/share/tool?canonicalUrl=http://www.example.com&caption=mycaption&posttype=photo&content=' + imageURL; 
      }, 
      error: function(){ 
      console.log('error'); 
      } 
    }); //ajax 
    }//callback 

});//svg 

}); //tumblrshare 

請幫助!

+0

默認行爲,爲什麼不使用'$(」 #tumblrshare')...'給b egin與?這個JS何時執行? –

+0

我可以,這是一回事。這只是一個加載在頁面底部的js文件。 – bjorkland

+0

回答

1

更改鏈接的「href」屬性不會在它已被點擊後更改其目標。考慮在您的ajax調用完成時使用window.location來重定向用戶。

$('body').on('click','#tumblrshare',function(e){ 
    e.preventDefault(); // Stop the default behaviour 
    ... 
    $.ajax({ 
    success: function(result){ 
     window.location = result.data.link; 
    }, 
    ... 
    }); 
... 
}); 
+0

所以,鏈接需要去的地方實際上是我的html中的 bjorkland

+0

是的,儘管在jquery中你想寫'$('#tumblrshare')。attr('href')'來獲得該值 –

+0

是的,我將它改爲$('#tumblrshare')。attr(' href'),鏈接仍在跳轉。我也嘗試使用e.stopPropogation,無濟於事。 – bjorkland

1
function loadTumblr(){ 
    var svg = $("#svg")[0]; 
    svg.toDataURL("image/png", { 
     callback: function(img) { 
      var img = img.replace("data:image/png;base64,", ""); 
      var imgurTitle = $("meta[property='og:title']").attr("content"); 
      $.ajax({ 
       url: 'https://api.imgur.com/3/image', 
       headers: {'Authorization': 'Client-ID **********'}, 
       type: 'POST', 
       data: {'image': img, 'type': 'base64', 'title': imgurTitle}, 
       success: function(result) { 
        imageURL = result.data.link; 
        window.location.href = 'https://www.tumblr.com/widgets/share/tool?canonicalUrl=http://www.example.com&caption=mycaption&posttype=photo&content=' + imageURL; 
       }, 
       error: function(){ 
        console.log('error'); 
       } 
      }); //ajax 
     }//callback 
    });//svg 
} 
+0

好主意,但也沒有工作! – bjorkland

+0

也許只是將它從一個錨點更改爲一個div,然後觸發一個運行您的AJAX調用的onClick事件。在成功函數內部,添加一個window.location = result.data.link; – Sage

+0

是的,我改變它爲一個按鈕,而我認爲這是幫助...現在唯一的問題是,window.location不會改變...讓我更新我的代碼上面。 – bjorkland

0

$("a.alate").on("click",function(event){ 
 
_thislink = $(this).attr("href"); 
 
event.preventDefault(); 
 
console.log(_thislink); 
 
$.ajax({ 
 
    type: 'get', 
 
    url : 'https://restcountries.eu/rest/v1/all', 
 
    fail:function(data) { 
 
    console.log("fail"); 
 
    } 
 
}).done(function(data) { 
 
    // when request finish 
 
    console.log(data); 
 
    // window.location = _thislink; 
 
}); 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<a class="alate" href="http://stackoverflow.com">Update Data then go</a>

在你的代碼中最重要的是使用

e.preventDefault(); 

停止的HREF

+0

是的,我一直在使用e.preventDefault(); (改變了上面的代碼),但它仍然在跳躍。 – bjorkland

+0

在這段代碼中它的跳轉請求完成後 // window.location = _thislink; 評論此線路不會 –

+0

現在試試這個代碼吧!運行代碼片段,看到它會帶來數據沒有去鏈接取消註釋去鏈接,然後它會去鏈接後帶數據 –

相關問題