お持ちの場合はyoursite/someroute
のようなURLが、あなたはnot found
エラーが発生します。したがって、rewriteを使用するこれらのルートに対してindex.phpを提供するようにApacheに指示する必要があります。
document.readyでは、URL(document.location.pathname
)を確認し、パスが何であるかに応じてコンテンツを読み込む必要があります。
インデックス・ページには、常にここで
は、あなたがこれを行うことができる方法のいくつかの簡単なサンプルコードでURL内のパス名に基づいて実際のコンテンツで置き換えられますロードスピナーが表示されるはずです:
//if path is /contact then return object with the url to php content page
// and title of the page
const pathToPage = path => {
switch (path) {
case "/contact":
return {
url:"/contact.php"
,title:"Contact"
,path:path
};
}
}
//when user navigates back and forward
window.addEventListener(
"popstate"
,event =>
//only set content, do not mess with history
onlyLoadmain(
pathToPage(location.pathname)
)
);
//load main content with or without messing with history
const loadMainBuilder = (push)=>(page)=> {
//if page is undefined then path is not of known content
if(page!==undefined){
if(push){
//set the url
history.pushState(
{},
page.title
,page.path
);
}
document.title = page.title
//@todo: should show loading here
//$('#main').html(loading);
$('#main').load(
'pages/' + page.url + '.php'
);
}
}
const loadMainAndPush = loadMainBuilder(true);
const onlyLoadmain = loadMainBuilder(false);
//... other code
$(document).ready(function() {
//load the content of current page
// when user gets a url in their email like yoursite/contact the
// apache rewrite rule will serve index.php but main content is
// showing loading untill your code actually replaces it with something
onlyLoadmain(pathToPage(location.pathname));
$('body').delegate('.navMenu', 'click', function (event) {
event.preventDefault();
//the element clicked should have href with the path, not the php file path
//so /contacts not /pages/contacts.php
loadMainAndPush(pathToPage($(this).attr('href')));
});
});
出典
2017-11-08 04:56:55
HMR
divの内部にロードされているすべてのページへのパスは、pagesというディレクトリ内にあります。私のajaxコードでは、パスとファイルのタイプ.... "href"に変更すると、ファイルと拡張子を指定する必要があることがわかりますか?また、読み込みイメージを指定するときに、ページを作成するか、イメージ自体をロードするだけです。 –
@DigiGodHymself読み込み中の画像をメインのPHPページの一部にするだけで、コンテンツが受信されるとJavaScriptがそのコンテンツに置き換えられます。 hrefはあなたがURLで見るものでなければならず、 'pathToPage'によってタイトルが何で、どのPHPページが読み込まれるべきかを知るために使われます。例えば、' http:// example.com/contact' hrefの値'/ contact'です – HMR