Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在我发布到facebook后,如何关闭弹出窗口?_Javascript_Jquery_Facebook_Sdk_Dialog - Fatal编程技术网

Javascript 在我发布到facebook后,如何关闭弹出窗口?

Javascript 在我发布到facebook后,如何关闭弹出窗口?,javascript,jquery,facebook,sdk,dialog,Javascript,Jquery,Facebook,Sdk,Dialog,在我们的博客上,我们有一个链接,用户可以将我们的文章发布到他们的时间线上。弹出窗口打开,用户发布到facebook,然后弹出窗口停留在那里并重定向到“www.oursite.com”。当用户完成发布或单击“取消”按钮时,我们如何关闭弹出窗口?根据《赫芬顿邮报》的说法,这是不可能做到的,但看看他们的代码,我们无法理解。 例如,这里的按钮将打开一个弹出窗口,然后在您发布文章或取消时关闭 以下是我们所拥有的: FB.init({appId: "90210", status: true, cookie:

在我们的博客上,我们有一个链接,用户可以将我们的文章发布到他们的时间线上。弹出窗口打开,用户发布到facebook,然后弹出窗口停留在那里并重定向到“www.oursite.com”。当用户完成发布或单击“取消”按钮时,我们如何关闭弹出窗口?根据《赫芬顿邮报》的说法,这是不可能做到的,但看看他们的代码,我们无法理解。 例如,这里的按钮将打开一个弹出窗口,然后在您发布文章或取消时关闭

以下是我们所拥有的:

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全部保留为空,但这是必需的)。

重定向到。然后在您网站的主页上,包括以下内容:

/* 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');
}
<SCRIPT>
window.close();
</SCRIPT>
if(window.location.hash='#close_window')window.close()

if(window.location.hash.indexOf(“#close_window”)!=-1)window.close();

由于facebook现在在散列后删除任何内容,并用post_id=xxxxx替换,似乎接受的答案不再有效

解决方案#1(如果您相信FB在不久的将来不会改变这一点):

解决方案#2(如果您想要更多的变化保险,并且不介意第二个文件): 创建新的html文件closewindow.html:

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

并在重定向中链接到它。

在花了一整天的时间研究这个问题之后,我有一个非常好的解决方案,我想与大家分享。我发现我可以通过手动打开自己的弹出窗口来完全避免使用带有FB.ui()的SDK。这样做时,重定向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');
}
<SCRIPT>
window.close();
</SCRIPT>
下面是一个使用上述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>

var dialog=新建FacebookFeedDialog(您的AppIDGoesher、您的DestinationUrlGoesher、您的CloseWindowUrlGoesher);
addParam('name','This is my title');
addParam('description','This is the description');
dialog.addParam('picture',yourImageUrlGoesher);
addParam('caption','This is the caption');
您的closeWindow html文件可以如下所示:

/* 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');
}
<SCRIPT>
window.close();
</SCRIPT>

window.close();

这不是对原始问题的直接回答,但它可能会帮助其他来到这里的人

有一种更可靠的方法可以做到这一点,允许您在共享成功完成后在原始页面上执行操作:

在原始页面中:

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...
  }
}
在页面中,您已将其设置为您的
重定向uri
,我通常会将其映射为
http://myserver.com/share/close

window.opener.shareCompleted();
现在,您可以确信共享窗口将关闭,如果需要,您将能够在原始页面上执行操作

注意:
shareCompleted
必须在根窗口名称空间中可用于此工作。如果您正在按应有的方式包装所有JavaScript,那么请确保:

window.shareCompleted = shareCompleted;
更新: 将此脚本添加到重定向页面:

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

只需从url中删除
redirect\u uri
参数


类似。

从2017年10月起,删除
重定向\u uri
似乎有效

它将默认为

谁的源代码就是


window.close()

也许这个答案可以帮助你:chrome不允许关闭其他脚本人打开的窗口谢谢,在与这个问题斗争了几个小时后,这是一个完美的解决方案!你救了我一天!!很高兴受到赞赏!这些天(2017年7月)这对任何人都有效吗?当我点击“这里”时,我可以发布,但窗口没有关闭,它只是停留在屏幕上