Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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 window.close不适用于Twitter网站_Javascript_Jquery - Fatal编程技术网

Javascript window.close不适用于Twitter网站

Javascript window.close不适用于Twitter网站,javascript,jquery,Javascript,Jquery,我有一个打开Twitter URL的代码(window.open()),但我认为Twitter正在清除/擦除我的窗口引用,这就是为什么我不能再控制我的窗口或使用window.close()关闭它。这方面的解决办法是什么 我尝试了console.log,如果网站加载,窗口总是空的。 代码如下: var windows = {}; function OpenWindow(id, url, title, w, h, l) { $.each(windows, function(index, val

我有一个打开Twitter URL的代码(window.open()),但我认为Twitter正在清除/擦除我的窗口引用,这就是为什么我不能再控制我的窗口或使用window.close()关闭它。这方面的解决办法是什么

我尝试了console.log,如果网站加载,窗口总是空的。 代码如下:

var windows = {};

function OpenWindow(id, url, title, w, h, l) {
  $.each(windows, function(index, value) {
    if (windows[index].closed) {
      $('#' + index + '').css("background-color", "");
    }
  });

  $('#' + id + '').css("background-color", "#F2F5F7");

  // Fixes dual-screen position Most browsers Firefox
  var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
  var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
  width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
  height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

  var left = ((width / 2) - (w / 2)) + dualScreenLeft;
  var top = ((height / 2) - (h / 2)) + dualScreenTop;

  var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + l);
  windows[id] = newWindow;

  // Puts focus on the newWindow
  if (window.focus) {
    newWindow.focus();
  }
}

//When I tried to close this window using this function
//It is no longer working and the window is null
function closeWindow(name) {
  var window = windows[name];
  if (window) {
    window.close();
    delete windows[name];
  }
}
这是因为twitter.com使用“同源”标题


由于您的页面不是来自同一来源,因此将创建一个新的顶级浏览上下文,您的浏览上下文将始终将此新浏览上下文视为已关闭,并且无法将其关闭。

网站不太可能更改您的变量。显示您的代码。@Barmar是的,可以清除窗口。这是在我使用window.open()打开Twitter URL时发生的。var窗口={};var newWindow=window.open(url,标题,'scrollbars=yes,宽度='+w+',高度='+h+',顶部='+top+',左侧='+l);windows[id]=newWindow;当我访问windows[id]时,窗口变量为空。将代码放入问题中,而不是注释中。如果窗口位于不同的域中,它可能无法访问脚本中的变量。请使用代码块,而不是图像。谢谢您的帮助。@IredhelPerez欢迎您。请看