純粋なjavascriptで単純なAjaxリクエストを使用すると、これを行うことができます。
// Container of external page contents
var container = document.querySelector('.container');
var loadWebpage = function (which) {
which = which.slice(1);
if (!which) return container.innerHTML = '';
var xhr = new XMLHttpRequest();
xhr.open('GET', which + '.html');
xhr.onload = function() {
if (xhr.status === 200) container.innerHTML = xhr.responseText;
else console.log(which + ' page not found!')
};
xhr.send()
};
window.onhashchange = function() {
loadWebpage(location.hash) // When hash changes load that page
};
loadWebpage(location.hash); // When page opens load current hash page
私はそれが役立つことを願って、幸運
ます。https:私は、インデックスページの
.container
要素に全体のコンテンツを入れ#about
と負荷about.html
のようなURLハッシュを取得する機能を書いた// developer.mozilla.org/en-US/docs/Web/API/History_API – epascarelloGoogle for AJAXをインデックスページに読み込み、epascarelloが提案したヒストリーAPIをご覧ください。 PushState、popStateなど... –
「シングルページアプリケーションでのルーティング」に関するGoogleの簡単な検索では、十分な量のリソースを使い始める必要があります。 –