正在删除/添加iframe src更改在浏览器上/向前点击Firefox(Quantum)

时间:2018-02-22 19:38:35

标签: javascript html iframe

当我点击按钮将源添加到iFrame中,然后单击浏览器的“后退”按钮时,它会从iFrame中删除源,就像它是本机“撤消”行为一样。

现在,即使我的文档上还有一个超过popstate的eventListener,它也不会首先到达那里。每当我第一次点击时,删除iframe src,然后第二次触发popstate。有时它甚至不会这样做。但这只发生在Firefox中。

Chrome正好相反,让我说我打开了一个新标签,访问了我的测试文件,点击按钮添加src,然后点击浏览器,它会带我到一个空白标签。但当然首先触发popstate

如何实现与Chrome相同的行为?它可行吗?

我不知道代码如何在Stackoverflow上运行,但是如果你想尝试并测试它,我建议你做一个本地的index.html文件。

JS:

window.onload = function() {

   window.addEventListener('popstate', function() {
      console.log('pop state!');
    });
};



function addSource() {
  window.history.replaceState(null, null, document.URL);
  $('#iframe').attr('src', 'https://www.youtube.com/embed/Zg8-AbXqv4M');
}

HTML:

<button onclick="addSource()">FRAME IT!</button>
<iframe id="iframe" src="" width="400" height="400" frameborder="0" allow="autoplay; encrypted-media"></iframe>

0 个答案:

没有答案