我認爲你正在尋找的腳本是pjax,它有你想要的所有功能。你可以找到它在github上:https://github.com/defunkt/jquery-pjax/tree/heroku
編輯
你可以用你喜歡的任何服務器端語言使用它。例如,
<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script src="jquery.pjax.js"></script>
<script type="text/javascript">
$(function(){
$('nav a').pjax('#content')
})
</script>
在您的網站的頭,
<nav>
<a href="otherContent1.php" >click me for other content</a>
<a href="otherContent2.php" >click me for even more additional content</a>
</nav>
<section id="content">
<h1>Original Content</h2>
<p>
this will be replaced by pjax with the content's of <a href="otherContent1.php">otherContent1.php</a> or <a href="otherContent2.php">otherContent2.php</a>
</p>
</section>
在主模板,修改你的PHP代碼,以便它會查找HTTP_X_PJAX
頭。如果存在(請參閱xhr.setRequestHeader('X-PJAX', 'true')
),僅渲染應替換<section id="content">
內容的部分,否則渲染整個頁面。 Pjax足夠智能,如果它工作,它將只取代<section id="content">
的內容,如果它不起作用,你仍然有正常的鏈接工作。如果你使用的話,pjax甚至會照顧谷歌分析,所以你的跟蹤仍然有效。
EDIT2:例
好了,在這裏你有一個樣本PHP頁面。請注意,我做了而不是測試了這個,我只是在堆棧溢出中快速而又髒地寫了它,以顯示你如何可以解決這個問題。此代碼是未經測試和肯定未生產準備。但是,作爲一個例子,你可以做這樣的事情(該文件應該被命名爲的index.php):
<?php
$render_template=true;
if ($_SERVER["HTTP_X_PJAX"])
{
$render_template=false;
}
?>
<?php
if ($render_template) {
?>
<html>
<head>
<meta charset="utf-8" />
<title>Testing Pjax</title>
<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script src="jquery.pjax.js"></script>
<script type="text/javascript">
$(function(){
$('nav a').pjax('#content')
})
</script>
</head>
<body>
Date in template: <?php echo $today = date("D M j G:i:s T Y"); ?>
<nav>
<a href="index.php?content=1" >click me for other content</a>
<a href="index.php?content=2" >click me for even more additional content</a>
</nav>
<section id="content">
<?php
}
?>
<?php
if ($_Get["content"]==1)
{
?>
<h1>Content=1</h1>
Date in content: <?php echo $today = date("D M j G:i:s T Y"); ?>
<p>
this will be replaced by pjax with the content's of <a href="index.php?content=1">index.php?content=1</a> or <a href="index.php?content=2">index.php?content=2</a>
</p>
<?php
} else {
?>
<h1>Content 2=2</h1>
Date in content: <?php echo $today = date("D M j G:i:s T Y"); ?>
<p>
this will be replaced by pjax with the content's of <a href="index.php?content=1">index.php?content=1</a> or <a href="index.php?content=2">index.php?content=2</a>
</p>
<?php
}
?>
<?php
if ($render_template) {
?>
</section>
</body>
</html>
<?php
}
?>
更好地爲您遇到的特定問題撰寫具體問題。 – dynamic 2012-02-13 15:14:16