I have a javascript function using which I display some content when the user clicks on a link.
function showContent(filename){
var oldcontent = $('#content').html();
var srcUrl = getSourceUrl(filename);
$('#content').html(oldcontent + '<iframe style="width:100%;height=100%;display:block;" src="'+srcUrl+'" />');
}
Since the content is loaded into an IFrame, the URL doesn't get changed and I can not go back to the previous page where the link was using browser's back button. I am a newbie and I tried to do something(silly experiment) like this. ie appending a '#page2' to the url but then when user clicks on the back button, nothing happens.
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+'" />');
Can anyone please tell me what's wrong with this and how to enable the back button functionality?
Try this:
<!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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.