이있는 경우 yoursite/someroute
같은 URL이 다음은 not found
오류가 발생합니다. 따라서 rewrite을 사용하는 경로에 대해 index.php를 제공하도록 아파치에 지시해야합니다.
document.ready에서 URL (document.location.pathname
)을 확인하고 경로가 무엇인지에 따라 내용을로드해야합니다.
//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')));
});
});
:
인덱스 페이지가 항상 URL을 여기에
에서 경로 이름에 따라 실제 내용으로 대체됩니다 로딩 스피너를 표시해야하는 것은 당신이 할 수있는 방법에 대한 몇 가지 간단한 예제 코드입니다
출처
2017-11-08 04:56:55
HMR
div 안에로드 된 모든 페이지의 경로는 pages라는 디렉토리 안에 있습니다. 내 아약스 코드에서 경로와 파일의 형식을 지정합니다 .... "href"로 변경하면 파일과 확장명을 지정해야합니다. 또한 로딩 이미지를 지정할 때 페이지로 만들거나 이미지 자체 만로드하면됩니다. –
@DigiGodHymself 로딩 이미지를 메인 PHP 페이지에 포함 시키면 컨텐츠가 수신되면 자바 스크립트가 컨텐츠로 바뀝니다. href는 url에있는 내용이어야하며'pathToPage'가 제목을 알아 내고 PHP 페이지를로드하는 데 사용됩니다. 예를 들어 'http : // example.com/contact'은 href의 값입니다. '/ contact'입니다. – HMR