【问题标题】:IFRAME and back / forward buttonIFRAME 和后退/前进按钮
【发布时间】:2011-10-06 23:40:16
【问题描述】:

我正在开发一个简单的 .hta 应用程序,它有一个控制面板和一个 IFRAME。

我添加了一个后退和前进按钮,但它们似乎不起作用。如果单击以下示例中的链接“a”和“b”,则后退和前进按钮不会执行任何操作。

如何做到这一点?

test.hta
===================================
<!DOCTYPE html>
<html>
<head>
    <title>Back / Forward Buttons</title>
    <hta:application id="test" applicationname="test" icon="res/icon.ico" showintaskbar="yes" singleinstance="yes">
</head>
<body>
    <div class="strip">
        <button onclick="output.history.back(); return false">Back</button>
        <button onclick="output.history.forward(); return false">Forward</button>
    </div>

    <div id="iframe-wrap" class="iframe-container">
        <iframe id="output" name="output" src="a.html" width="100%" border="0" frameborder="no" scrolling="yes"></iframe>
    </div>
</body>
</html>

a.html
===================================
<!DOCTYPE html>
<html>
    <head><title>A</title></head>
    <body>PAGE A - <a href="b.html">Go to B</a></body>
</html>

b.html
===================================
<!DOCTYPE html>
<html>
    <head><title>B</title></head>
    <body>PAGE B - <a href="a.html">Go to A</a></body>
</html>

【问题讨论】:

    标签: javascript internet-explorer iframe browser-history hta


    【解决方案1】:

    试试:

    window.frames.output.history.forward();
    

    window.frames.output.history.go(+1);
    

    可能更好的是使用 getElementByID 来获取您尝试使用历史记录的元素。

    iFrame 的历史记录也存在一些已知的跨浏览器问题,但我现在不记得具体是什么,但 Google 应该能够为您解答。

    【讨论】:

      【解决方案2】:

      我最终不得不使用以下方法手动跟踪 iFrame 中的页面更改:

          var iFrameHistory = {
              history : [],
              pos     : 0,
              ignore  : false,
      
              updateUI: function() {
                  var el;
      
                  // Enable / disable back button?
                  el = document.getElementById('back');
                  if (iFrameHistory.pos === 1)
                      el.className = 'disabled';
                  else
                      el.className = '';
      
                  // Enable / disable forward button?
                  el = document.getElementById('forward');
                  if (iFrameHistory.pos >= iFrameHistory.history.length)
                      el.className = 'disabled';
                  else
                      el.className = '';
              },
      
              back: function() {
                  var newPos = Math.max(1, this.pos - 1);
                  if (newPos !== this.pos) {
                      this.pos = newPos;
                      this.ignore = true;
                      document.getElementById('output').src = this.history[ this.pos - 1 ];
      
                      this.updateUI();
                  }
              },
              forward: function() {
                  var newPos = Math.min(this.history.length, this.pos + 1);
                  if (newPos !== this.pos) {
                      this.pos = newPos;
                      this.ignore = true;
                      document.getElementById('output').src = this.history[ this.pos - 1 ];
      
                      this.updateUI();
                  }
              },
              reload: function() {
                  document.getElementById('output').contentWindow.location.reload();
              },
              onload: function() {
                  if (!this.ignore) {
                      var href = document.getElementById('output').contentWindow.location.href;
                      if (href !== this.history[ this.pos - 1 ]) {
                          this.history.splice(this.pos, this.history.length - this.pos);
                          this.history.push(href);
                          this.pos = this.history.length;
      
                          this.updateUI();
                      }
                  }
                  else {
                      this.ignore = false;
                  }
              }
          };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        相关资源
        最近更新 更多