修改iframe.src而不输入历史记录对象 [英] Modify iframe.src without entry to history object

查看:362
本文介绍了修改iframe.src而不输入历史记录对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有一个iframe。我通过javascript修改src属性,如下所示:

I have an iframe in my web page. I modify the src property via javascript like so:

document.getElementById('myiframe').src = 'http://vimeo.com/videoid1';
document.getElementById('myiframe').src = 'http://vimeo.com/videoid2';
document.getElementById('myiframe').src = 'http://vimeo.com/videoid3';

然而,每次我这样做,它都会记录在浏览器的历史记录中。因此,每次我在浏览器窗口中按下时,iframe内容都会从videoid3转到videoid2到videoid1。如果我再次按回,整个页面都会返回。

However, everytime I do this, it's logged into the browser's history. So everytime I press back in the browser window, the iframe content goes from videoid3 to videoid2 to videoid1. If I press back again, the entire page goes back.

我想用javascript修改iframe src而不在浏览器的历史记录中记录条目。因此,如果我单击浏览器后退按钮,整个页面将返回而不更新iframe。

I would like to modify the iframe src with javascript WITHOUT logging an entry into the browser's history. So if i click the browser back button, the entire page goes back without updating the iframe.

我尝试过这样的事情:

document.getElementById('myiframe').contentWindow.location.replace('http://vimeo.com/videoid1');
document.getElementById('myiframe').contentWindow.location.replace('http://vimeo.com/videoid2');
document.getElementById('myiframe').contentWindow.location.replace('http://vimeo.com/videoid3');

虽然这使得浏览器后退按钮按照我想要的方式运行,但它破坏了vimeo中的某些内容视频。 Vimeo要求您通过iframe.src而不是contentWindow.location.replace()来更改URL。

Although this made the browser back button behave the way I wanted to, it broke certain things in the vimeo video. Vimeo REQUIRES you to change urls via the iframe.src instead of contentWindow.location.replace().

因此,如何在不登录的情况下修改iframe.src历史?

As such, how do I modify the iframe.src WITHOUT logging into history?

相关
这实际上是我正在探索解决主要问题的解决方案之一,我在这里发布带有iframe的历史对象后退按钮

推荐答案

不要更改src,只需用新的iframe替换旧的iframe?

don't change the src, just replace the old iframe with a new one?

<!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 />

没有历史记录。功能相同。每个人都赢了。

No history states. Same functionality. Everyone wins.

这篇关于修改iframe.src而不输入历史记录对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆