2013-02-06 37 views
6

我在我的网页中有一个iframe。我通过JavaScript修改src属性,如下所示:修改iframe.src,但没有输入历史记录对象

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。如果我再次按下,整个页面会返回。

我想用javascript修改iframe src,但没有记录进入浏览器的历史记录。因此,如果我点击浏览器后退按钮,整个页面会返回而不更新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()来更改网址。

因此,如何修改iframe.src而不登录历史记录?

相关 其实,这是我在探索解决的主要问题的解决方案之一,我在这里张贴History object back button with iframes

+0

可能的重复http://stackoverflow.com/questions/821359/reload-an-iframe-without-adding-to-the-history我认为这个答案将解决你的问题http://stackoverflow.com/a/8681618/2009041 –

回答

14

不改变SRC,只是用一个新的代替旧的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 /> 

没有历史状态。相同的功能。每个人都赢。

+0

如果你愿意,你可以发布这个答案给我提到的其他问题。我会接受它。 – John