繁体   English   中英

将内容加载到iframe中后,浏览器后退按钮不起作用

[英]Browser back button doesn't work when content is loaded into an iframe

我有一个javascript函数,当用户单击链接时,它会显示一些内容。

function showContent(filename){
  var oldcontent = $('#content').html();
  var srcUrl = getSourceUrl(filename);
  $('#content').html(oldcontent + '<iframe style="width:100%;height=100%;display:block;" src="'+srcUrl+'" />');
}

由于内容已加载到IFrame中,因此URL不会更改,并且无法使用浏览器的“后退”按钮返回到该链接所在的上一页。 我是新手,因此尝试做类似这样的事情(愚蠢的实验)。 例如,在网址后附加一个“#page2”,但是当用户单击“后退”按钮时,没有任何反应。

var oldcontent = $('#content').html();
var srcUrl = getSourceUrl(filename);
location.href = location.href + '#page2';
$('#content').html(oldcontent + '<iframe style="width:100%;height=100%;display:block;" src="'+srcUrl+'" />');

谁能告诉我这是什么问题以及如何启用后退按钮功能?

尝试这个:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Hash Test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
  </head>
  <body>
    <div id='message-container'></div>
  </body>
  <script>
    var messages = ["Hello. Click anywhere on the page.", "How are you?", "I hope you're fine.", "Have a nice day and hit the back button."];
    var currentMessage = 0;
    var lastMessage = messages.length - 1;

    $(document).click(function() {
      nextMessage();
    });

    function nextMessage() {
      if (currentMessage < lastMessage) {
        currentMessage++;
        updateHash();
      }
    }

    function displayMessage() {
      $("#message-container").text(messages[currentMessage]);
    }

    function updateHash() {
      window.location.hash = "#message" + currentMessage;
    }

    $(window).on("hashchange", function() {
      if (/message\d+/.test(window.location.hash)) {
        currentMessage = window.location.hash.match(/message(\d+)/)[1];
        displayMessage();
      }
    });

    updateHash();
  </script>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM