我认为你正在寻找的脚本是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