2013-10-04 70 views
0

預期我有一個共享鏈接功能,下面的代碼:的window.open不工作在iPad上的Safari

longurl = "www.google.com" 
    var newWin = window.open('', share_win_name, 'width=826,height=836') 
    gapi.client.load('urlshortener', 'v1', function() { 
    var request = gapi.client.urlshortener.url.insert({ 
     'resource': { 
      'longUrl': longurl 
     } 
    }); 
    var resp = request.execute(function(resp) { 
     if (resp.error) { 
     newWin.location = share_link + encodeURIComponent(longurl) 
     } else { 
     newWin.location = share_link + encodeURIComponent(resp.id) 
     } 
    }); 
    }); 

此代碼在桌面上。但在iPad上的Safari

  • 一個空白的新標籤頁打開什麼也沒有它
  • 當我去一些其他選項卡,然後回來這個選項卡,然後我看到了新的窗口是否 刷新/重載

我相信它是一個問題,因爲我嘗試先打開一個空白窗口,然後使用鏈接信息進行更新。

解決方案是什麼?

回答

0

只要你做到以下幾點:

newWin = window.open('', share_win_name, 'width=826,height=836'); 

你正在創建一個彈出了。

除了在範例之外聲明newWin變量之外,還沒有實例化彈出窗口。

在回調函數中,實例化彈出窗口。

longurl = "www.google.com" 
var newWin; 
gapi.client.load(...); 
var resp = request.execute(function(resp) { 
    if (resp.error) { 
     newWin = window.open(share_link + encodeURIComponent(longurl), share_win_name, 'width=826,height=836'); 
    } else { 
     newWin = window.open(share_link + encodeURIComponent(resp.id), share_win_name, 'width=826,height=836'); 
    } 
    }); 
}); 

讓我們來看一些冗餘代碼。 最好安全,不要抱歉。