С вашего вопроса я понимаю, что вам нужно перейти на другую страницу, не обновляя страницу как Google.
Но это не делается с использованием кеша. Для этого есть функция, называемая pushState()
и onpopstate()
.
Как это работает?
С функцией pushState()
мы можем изменить URL-адрес страницы, не обновляя страницу. И предыдущий url автоматически добавится в историю. Поэтому нам нужно реализовать одну страницу для загрузки содержимого страницы с помощью ajax на основе URL-адреса.
Функция onpopstate()
будет активирована при изменении URL-адреса с использованием pushState()
.
Пример:
http://html5.gingerhost.com/
выше страница использует описанную выше функцию. Эта страница состоит из 4 ссылок (Home, Seattle, New York, London). Когда мы нажимаем на эту ссылку, новый URL-адрес будет загружен без обновления страницы.
<title></title>
<ul id="menu" class="clearfix">
<li class="current"><a href="/">Home</a></li>
<li><a href="/seattle">Seattle</a></li>
<li><a href="/new-york">New York</a></li>
<li><a href="/london">London</a></li>
</ul>
<article>
<h1></h1>
<div id="image"></div>
<div id="articletext"></div>
<div class="clear"></div>
</article>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script>
// THIS IS WHERE THE MAGIC HAPPENS
$(function() {
//Capture the click on the links
$('#menu a').click(function(e) {
href = $(this).attr("href");
// Call a function to load the content based on the url
loadContent(href);
// HISTORY.PUSHSTATE add the url to the history
history.pushState('', 'New URL: '+href, href);
e.preventDefault();
});
// THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
window.onpopstate = function(event) {
console.log("pathname: "+location.pathname);
// Load the content on clicking back or forward using its url
loadContent(location.pathname);
};
});
function loadContent(url){
// USES JQUERY TO LOAD THE CONTENT PROVIDED BY content.php
$.getJSON("content.php", {cid: url, format: 'json'}, function(json) {
// output provider by content.php like {"title":"Seattle - Part of a demo for #ProSEO","h1":"Seattle","article #articletext":"<p>Seattle is the northernmost major city in the contiguous United States, and the largest city in the Pacific Northwest and the state of Washington. It is a major seaport situated on a narrow isthmus between Puget Sound (an arm of the Pacific Ocean) and Lake Washington, about 114 miles (183 km) south of the Canada - United States border, and it is named after Chief Sealth \"Seattle\", of the Duwamish and Suquamish native tribes. Seattle is the center of the Seattle-Tacoma-Bellevue metropolitan statistical area--the 15th largest metropolitan area in the United States, and the largest in the northwestern United States.<\/p><p>Seattle is the county seat of King County and is the major economic, cultural and educational center in the region. The 2010 census found that Seattle is home to 608,660 residents within a metropolitan area of some 3.4 million inhabitants. The Port of Seattle, which also operates Seattle-Tacoma International Airport, is a major gateway for trade with Asia and cruises to Alaska, and is the 8th largest port in the United States in terms of container capacity.<\/p>","#image":"<img class=\"thumbnail\" alt=\"\" src=\"seattle.jpg\">"}
// THIS LOOP PUTS ALL THE CONTENT INTO THE RIGHT PLACES(values will be set)
$.each(json, function(key, value){
$(key).html(value);
});
});
}
</script>
Как как выше вы можете в состоянии оказать содержание и дать ответ JSon и передать его на JQuery, и вы можете в состоянии достигнуть этого.