同じヘッダーとフッターを含む3ページ間を移動しようとしましたが、各ページの内容は異なります。 ハッシュ変更時に異なる内容のHTMLをロードしたい。 問題は、同じページを再度クリックすると、content.htmlが再度読み込まれることです。ページ間のナビゲーション
Javaスクリプト/ html/jqueryを使用して、HTMLを何度も読み込まずにコンテンツを使用するにはどうすればよいですか?
コード例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>One Page Test</title>
<link rel="stylesheet" type="text/css" href="css/homepage.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="main-container" class="main-container">
<div id="header" class="header">
</div>
<div id="content" class="content"></div>
<div id="footer" class="bot"></div>
</div>
<script>
document.onreadystatechange = function(){
if (document.readyState == 'complete') {
window.onhashchange=hash_change;
window.onload=hash_change;
if(window.location.hash==''){
//default hash
window.location.replace('#home.html');
}
//load the header
$("#header").load("fragments/navigationbar.html");
//load the footer
$("#footer").load("fragments/footer.html");
}
}
function hash_change()
{
//get the new hash
var newHashCode = window.location.hash.substring(1);
if (newHashCode === "home.html"){
$("#content").load("home.html");
} else if (newHashCode === "aboutus.html") {
$("#content").load("aboutus.html");
} else if (newHashCode === "contact.html"){
$("#content").load("contact.html");
}
}
</script>
</body>
</html>
JSナビゲーションを使用して、通常のHTMLで簡単にナビゲートできるのはなぜですか? – Troyer
JSコンソールを確認すると、エラーが表示されることがあります。 –