2015-11-10 89 views
1

我有一个设置了iframe的网页。我也有4个按钮设置在Iframe下面,这将导致它显示一个特定的页面,具体取决于按下哪个按钮。记住iframe页面刷新时

简单的东西。

我的问题是,我需要页面每5分钟自动刷新一次。这会重置Iframe。是否有可能强制Iframe保持它在页面的其余部分刷新它的位置?或者我可以让Iframe'记住'刷新的位置?

HTML参考 -

<iframe src="Kine-Yield-Data/KG4x10.htm" name="iframe_a" frameborder="0" width="100%" height="100"></iframe> 
<br> 

<a href="Kine-Yield-Data/KG4x10.htm" target="iframe_a" class="button">10</a> 
&nbsp; 
<a href="Kine-Yield-Data/KG4x25.htm" target="iframe_a" class="button">25</a> 
&nbsp; 
<a href="Kine-Yield-Data/KG4x30.htm" target="iframe_a" class="button">30</a> 
&nbsp; 
<a href="Kine-Yield-Data/KG4x50.htm" target="iframe_a" class="button">50</a> 

我风格的链接的按钮。

+0

它有趣的是,谷歌搜索只发现人们问我想要的相反。我猜想我很难过! – Jamsandwich

回答

2

那么,你可以使用JavaScript来做到这一点。

当您按住点击事件时,您可以保存帧路径并在“刷新”时将此值添加到您的URL中,然后在您的iframe中选择此值并再次设置。我做了一个脚本,一起来看看:

var currentFramePath = ''; 
var iframe = '<iframe src="{src}" name="iframe_a" id="iframe_a "frameborder="0" width="100%" height="100"></iframe>'; 

      $(document).ready(function(){ 

       var urlFrame = getUrlParameter('currentFrame'); 

       if(urlFrame != null && urlFrame != ''){ 
        $('#iframeContainer').html(iframe.replace('{src}', urlFrame)); 
        currentFramePath = urlFrame; 
       } 

       $('.button').click(function(){ 
        currentFramePath = $(this).attr('href'); 
       }); 

       setInterval(function(){ 
        window.location = window.location.href.split('?')[0] + '?currentFrame=' + currentFramePath; 
       }, 5000); 

      }); 

      function getUrlParameter(sParam) { 
       var sPageURL = decodeURIComponent(window.location.search.substring(1)), 
        sURLVariables = sPageURL.split('&'), 
        sParameterName, 
        i; 

       for (i = 0; i < sURLVariables.length; i++) { 
        sParameterName = sURLVariables[i].split('='); 

        if (sParameterName[0] === sParam) { 
         return sParameterName[1] === undefined ? true : sParameterName[1]; 
        } 
       } 
      }; 

HTML:

<div id='iframeContainer'> 
     <iframe src="Kine-Yield-Data/KG4x10.htm" name="iframe_a" id="iframe_a "frameborder="0" width="100%" height="100"></iframe> 
    </div> 
<br> 

<a href="Kine-Yield-Data/KG4x10.htm" target="iframe_a" class="button">10</a> 
&nbsp; 
<a href="Kine-Yield-Data/KG4x25.htm" target="iframe_a" class="button">25</a> 
&nbsp; 
<a href="Kine-Yield-Data/KG4x30.htm" target="iframe_a" class="button">30</a> 
&nbsp; 
<a href="Kine-Yield-Data/KG4x50.htm" target="iframe_a" class="button">50</a> 

编辑:

谷歌浏览器不允许你改变IFRAME SRC属性,因此,一我发现这样做的方法是用新的SRC值重新创建元素。

我希望它能帮助你。

+0

感谢Rodrigo的快速回复。正在处理一些其他的错误处理,但我今天将讨论这个问题。我会让你知道它是怎么回事。 – Jamsandwich

+0

这是一段很棒的剧本,非常感谢!在我当前的项目过程中,我学到了大量关于VBA和HTML(加上CSS)的知识。我已经使用了从其他地方复制的JS的一些部分,但希望我将来有机会使用它。 这是非常有用的,但看起来如此复杂。有一天我会学习它... – Jamsandwich