浏览器在页面本身之前作用于嵌套的iframe - 有没有办法避免它?

kfi*_*roo 29 html javascript ajax iframe back-button

我有一个页面,其中包含一些ajax和大量javascript加载的动态数据.

该页面包含一个列表,用户可以从中选择,每个选定的值都会将新数据加载到页面中.

其中一个数据项是提供给iframe的网址.

我使用jQuery BBQ:Back Button&Query Library来模拟浏览器返回行为.

除了我第一次单击后退按钮iframe返回其先前位置然后我需要再次单击以使页面返回时,一切正常.

有没有办法禁用iframe的后退行为?

kfi*_*roo 56

我发现我的问题的答案猜测它可能对其他人有用.

问题在于我为我的Iframe分配了新的URL,我使用了Jquery,所以它看起来像这样:

$('#myIFrame').attr('src',newUrl);
Run Code Online (Sandbox Code Playgroud)

以这种方式分配URL时,它会向浏览器的访问URL列表添加一个新条目以返回.
这不是理想的行为,所以在一些谷歌搜索后我发现你可以为Iframe对象分配一个新的URL而不将它添加到'后退列表',它看起来像这样:

var frame = $('#myIFrame')[0];  
frame.contentWindow.location.replace(newUrl);
Run Code Online (Sandbox Code Playgroud)

这样我的后退按钮就像预期的那样.

顺便说一句,我从这里得到了答案.

希望这对你有帮助,就像对我一样.

  • 这仅适用于与嵌入网站具有相同域名的iframe.不适用于跨域iframe. (8认同)

Geo*_*der 5

如果您尝试为 IFrame 设置跨域 URL,则接受的答案似乎不起作用。作为一种解决方法,我在设置src(使用 jQuery)之前从 DOM 中分离了 IFrame 。

// remove IFrame from DOM before setting source, 
// to prevent adding entries to browser history
var newUrl = 'http://www.example.com';
var iFrame = $('#myIFrame');
var iFrameParent = iFrame.parent();

iFrame.remove();
iFrame.attr('src', newUrl);
iFrameParent.append(iFrame);
Run Code Online (Sandbox Code Playgroud)