2016-12-09 44 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

现在试试这个代码吧!运行代码片段,看到它会带来数据没有去链接取消注释去链接,然后它会去链接后带数据 –

相关问题