2013-01-11 51 views
0

我使用的是劇本我found here動態生成短鏈接,我的資料Tweet按鈕,它工作得很好,但我不能似乎做的唯一的事情就是創建一個鏈接打開無論是新標籤還是最好是一個彈出窗口。Bitly API - JavaScript函數打開新窗口

我已經試過了腳本的window.location的部分的一些變化,但到目前爲止我沒有運氣。如果有人能夠直接指出我的話,我會非常感激。

這是我使用的腳本...

<script> 
    var TweetThisLink = { 
     shorten: function(e) { 
      // this stops the click, which will later be handled in the response method 
      e.preventDefault(); 
      // find the link starting at the second 'http://' 
      var url = this.href.substr(this.href.indexOf('http:', 5)); 
      BitlyClient.shorten(url, 'TweetThisLink.response'); 
     }, 

     response: function(data) { 
      var bitly_link = null; 
      for (var r in data.results) { 
       bitly_link = data.results[r]['shortUrl']; 
       break; 
      } 
      var tweet_text = "Text for the Tweet goes here" 

      window.location = "http://twitter.com/home?status=" + encodeURIComponent(tweet_text + ' ' + bitly_link + " #Hashtag1 #Hashtag2"); 

     } 
    } 

    jQuery('.tweetlink').bind('click', TweetThisLink.shorten); 
</script> 

提前:)

回答

1

也許非常感謝你正在尋找

window.open("http://example.com"); 
3

通常情況下,你可以只是做window.open

window.open("http://twitter.com/home?status=" + encodeURIComponent(tweet_text + ' ' + bitly_link + " #Hashtag1 #Hashtag2"); 

但是,因爲你正在做一個Ajax調用此發生之前,有機會,這個窗口彈出會被瀏覽器攔截,因爲window.open命令不再與點擊有關(瀏覽器允許前window.open命令跌倒在一定時間在未啓動的「彈出」下)。

一個解決辦法是先打開上單擊窗口(在縮短功能):

var win = window.open('about:blank'); 

,然後重定向在你的響應函數:

win.location = 'http://twitter.com/etc...'; 

演示:http://jsbin.com/usovik/1

+0

嗨大衛,非常感謝您的回答,但不幸的是它並不適合我。請原諒我,因爲我的JS技能是微不足道的,但在縮短函數的地方,你會建議我放置window.open?並在響應中的win.location同樣呢?然後,我仍然使用jquery綁定來加入縮短的鏈接? – robolist