【发布时间】:2018-01-26 11:19:20
【问题描述】:
我正在使用一个 iframe 组件来加载通过 props 传递的跨域 url。通过 props 将 iframe 导航到新 url 会将新条目推送到 iframe 的历史记录中,这会劫持浏览器的“后退”按钮。这是跨域 iframe 的预期行为,只要它们的 src url 发生变化。
那么,是否有可能简单地创建一个新的 iframe 而不是更改 src?我的假设是,这会破坏 iframe 的任何内部状态,从而阻止新条目进入“后退”按钮的历史记录。
iframe 组件(改编自react-iframe):
// ETA: using React.Component instead of
// PureComponent yields the same back button behavior
const Iframe = class extends PureComponent {
render() {
return React.createElement('iframe', {
ref: 'iframe',
frameBorder: '0',
src: this.props.url,
sandbox: this.props.sandbox,
referrerpolicy: 'no-referrer',
target: '_parent',
allowFullScreen: this.props.allowFullScreen || false,
style: Object.assign(
{},
{
position: this.props.position || 'absolute',
display: this.props.display || 'block',
height: this.props.height || '100%',
width: this.props.width || '100%'
},
this.props.styles || {}
),
height: this.props.height || '100%',
width: this.props.width || '100%'
});
}
};
ETA:我假设创建一个新的底层 iframe 将阻止进入历史记录 based on this answer,内容如下:
不要更改src,用新的iframe替换旧的iframe? 没有历史状态。相同的功能。每个人都赢了。
<!doctype html>
<style> iframe {
width: 300px;
height:300px; } </style>
<script> var urls = ["http://bing.com", "http://google.com",
"http://duckduckgo.com"];
function next() {
if(urls.length==0) return;
var original = document.getElementsByTagName("iframe")[0];
var newFrame = document.createElement("iframe");
newFrame.src = urls.pop();
var parent = original.parentNode;
parent.replaceChild(newFrame, original); } </script>
<p>let's test some iframes</p> <button onclick="next()">next</button>
<iframe />
【问题讨论】:
标签: javascript html reactjs iframe