2013-03-02 27 views
7

在我們的博客上,我們有一個鏈接,用戶可以將我們的文章發佈到他們的時間表。彈出窗口打開,用戶發佈到facebook,然後彈出窗口停留在那裏並重定向到「www.oursite.com」。當用戶完成發佈或單擊取消按鈕時,我們該如何關閉彈出窗口?根據this so question它不能完成,但赫芬頓郵報已經找到了它,但看着他們的代碼,我們無法弄清楚。 作爲一個例子,the facebook share按鈕在這裏將打開一個彈出窗口,然後在您發佈文章或取消時關閉。如何在發佈到Facebook後關閉彈出窗口?

下面是我們所擁有的:

FB.init({appId: "90210", status: true, cookie: true}); 

     function postToFeed() { 

     // calling the API ... 
     var obj = { 
      method: 'feed', 
      redirect_uri: 'http://www.oursite.com/', 
      link: 'http://www.oursite.com/', 
      picture: 'http://www.oursite.com/png.png', 
      name: 'Some title', 
      caption: '', 
      description: '' 
     }; 

     function callback(response){ 
      window.close(); // doesn't do anything 
      //document.getElementById('msg').innerHTML = "Post ID: " + response['post_id']; 
     } 

     FB.ui(obj, callback); 
     } 

我們已經嘗試添加window.close()的;在回調(還有self.close();)中,嘗試將redirect_uri留空(並嘗試將redirect_uri全部取出,但它是必需的)。

+0

也許這個回答可以幫助你:http://stackoverflow.com/a/2520861/1427942 – Eich 2013-03-02 23:43:00

回答

15

重定向到http://oursite.com/#close_window。然後在您的網站的主頁上,包括這樣的內容:

if (window.location.hash == '#close_window') window.close();

+2

無法在Chrome工作 – Manuel 2014-05-28 12:49:24

+0

Chrome不允許關閉已被其他腳本打開的窗口 – levi 2015-11-04 21:47:44

0
<script>if (window.location.hash.indexOf("#close_window") != -1) window.close();</script> 
17

看來,接受的答案不再工作,由於這樣的事實,現在Facebook的哈希條後,任何與POST_ID = XXXXX替換它。

解決方案#1(如果您信任FB不改變這個在不久的將來):

if(window.location.search.indexOf('post_id')==1) window.close(); 

解決方案2(如果你想對變化多一點保險,不介意第二個文件): 創建一個新的HTML文件closewindow.html:

<html><body><script>window.close()</script></body></html> 

,並鏈接到它的重定向。

+0

解決方案#2很棒,大聲​​笑,謝謝。 – Lauro182 2017-04-06 00:48:01

8

花了一整天的時間研究這個問題後,我有一個很好的解決方案,我想分享。與FB.ui()一起使用SDK,我發現我可以通過手動打開自己的彈出框到https://www.facebook.com/dialog/feed來完全避免它。當這樣做時,redirect_uri按預期工作。只要您需要用戶點擊一個按鈕來彈出對話框,就不會觸發彈出式窗口攔截器。

我不認爲這個代碼有任何妥協,如果有的話,它比實際的SDK更容易使用。

我的JavaScript代碼(可以保存爲FacebookFeedDialog.js)看起來是這樣的:

/* by Steven Yang, Feb 2015, originally for www.mathscore.com. This code is free for anybody to use as long as you include this comment. */ 
function FacebookFeedDialog(appID, linkTarget, redirectTarget) { 
    this.mParams = { 
    app_id: appID, 
    link: linkTarget, 
    redirect_uri: redirectTarget, 
    display: "popup" 
    } 
}; 

/* Common params include: 
    name - the title that appears in bold font 
    description - the text that appears below the title 
    picture - complete URL path to the image on the left of the dialog 
    caption - replaces the link text 
*/ 
FacebookFeedDialog.prototype.addParam = function(key, value) { 
    this.mParams[key] = value; 
}; 

FacebookFeedDialog.prototype.open = function() { 

    var url = 'https://www.facebook.com/dialog/feed?' + encodeCGIArgs(this.mParams); 
    popup(url, 'feedDialog', 700, 400); 
}; 

/* Takes a param object like this: 
    { arg1: "value1", arg2: "value2" } 
    and converts into CGI args like this: 
    arg1=value1&arg2=value2 

    The values and args will be properly URI encoded 
*/ 
function encodeCGIArgs(paramObject) { 

    var result = ''; 

    for (var key in paramObject) { 
    if (result) 
     result += '&'; 
    result += encodeURIComponent(key) + '=' + encodeURIComponent(paramObject[key]); 
    } 

    return result; 
} 

function popup(mylink,windowname,width,height) { 
    if (!window.focus) return; 
    var href; 
    if (typeof(mylink) == 'string') 
    href=mylink; 
    else 
    href=mylink.href; 
    if (!windowname) 
    windowname='mywindow'; 
    if (!width) 
    width=600; 
    if (!height) 
    height=350; 
    window.open(href, windowname, 'resizable=yes,width='+width+',height='+height+',scrollbars=yes'); 
} 

下面是一個使用上面的Javascript代碼示例HTML文件:

<HTML> 
<BODY> 
<SCRIPT type="text/javascript" src="FacebookFeedDialog.js"></SCRIPT> 
<SCRIPT> 
var dialog = new FacebookFeedDialog(yourAppIDGoesHere,yourDestinationURLGoesHere,yourCloseWindowURLGoesHere); 
dialog.addParam('name','This is my title'); 
dialog.addParam('description','This is the description'); 
dialog.addParam('picture',yourImageURLGoesHere); 
dialog.addParam('caption','This is the caption'); 
</SCRIPT> 

<A href="javascript:dialog.open()">Open facebook dialog</A> 
</BODY> 
</HTML> 

你closeWindow HTML文件可能看起來像這樣:

<SCRIPT> 
window.close(); 
</SCRIPT> 
+0

謝謝你,與此戰鬥了幾個小時後,一個完美的解決方案!你救了我的一天! – 2015-11-09 22:41:23

+0

非常感謝! – Steven 2015-11-10 23:32:29

1

這不是直接回答th原來的問題,但它可能有助於其他人到達這裏。

還有就是要做到這一點更健壯的方式,可以讓你把原始頁面上操作時的份額已成功完成:

在原始頁面:

var shareWindow; 

function openShareWindow() { 
    // See https://developers.facebook.com/docs/sharing/reference/share-dialog 
    shareWindow = window.open('https://www.facebook.com/dialog/share?app_id=...&display=popup&redirect_uri=...'); 
} 

function shareCompleted() { 
    if (shareWindow) { 
    shareWindow.close(); 
    shareWindow = null; 

    // The share was successful, so do something interesting here... 
    } 
} 

在你的頁面設置爲您redirect_uri,我通常映射到類似http://myserver.com/share/close

window.opener.shareCompleted(); 

現在你放心份額窗口將關閉,如果需要,您可以在原始頁面上執行操作。

注意:shareCompleted必須在此工作的根窗口名稱空間中可用。如果你包裹所有的JavaScript,你是應該的,那麼一定要:

window.shareCompleted = shareCompleted; 
3

UPDATE: 添加這個腳本到您的重定向頁面:

if (document.referrer == "https://www.facebook.com/" && (window.location.href.indexOf('post_id=') != -1 || window.location.hash == '#_=_')) { 
    window.close(); 
} 
+0

它工作正常:) – 2016-08-17 06:11:59

2

剛剛從刪除redirect_uri參數網址。

Like here

+0

這是最近這個工作(2017年7月)?當我點擊'這裏'我可以發佈,但窗口不關閉,它只是在https://www.facebook.com/dialog/return/close#_=_ – 2017-08-01 11:44:09