打开选项卡并获取pagesource

时间:2018-03-03 14:48:31

标签: javascript

我正在尝试打开一个新标签页,并在控制台中记录page_source。

function getRespons(autor, message) {
  var urlstring = "?a=" + autor + "&m=" + message;
  var encodedurl = encodeURI(urlstring);
  var win = window.open('http://127.0.0.1/gate.php' + encodedurl, '_blank');
  if (win) {
    //Browser has allowed it to be opened
    win.focus();
    console.log(window.document.body.innerHTML);
    win.close();
  } else {
    //Browser has blocked it
    alert('Please allow popups for this website');
  }
}

控制台中正在打印一些HTML,但这是第一个选项卡的HTML而不是最近打开的选项卡。

1 个答案:

答案 0 :(得分:0)

您可以使用setTimeout方法。

          if (win) {
            //Browser has allowed it to be opened
            win.focus();
            setTimeout(function(){ 
               console.log(win.document.body.innerHTML);
                win.close();
            }, 5000);
         }