2012-02-13 81 views
2

在過去幾天我漫步在網絡上的腳本,將有我希望的元素..最好的因此是這一個http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp,但有很多問題,我需要更好的解決方案:加載動態PHP和MySQL頁面無需刷新 - AJAX

  • 我不想要標籤:我想要所有鏈接加載頁面像文件夾(www.site.com/first/site/)。這在github上很神奇(點擊這裏https://github.com/spjoe/deluge-yarss-plugin,然後點擊一個隨機文件,然後回頭看看地址欄)。如果有人知道他們在做什麼或如何做,這將非常酷。

  • 我需要動態內容:上面的腳本只從開關函數中加載「個案」,而我需要php來檢查url並在mysql數據庫中找到一個文件並根據url從所需表中回顯內容,如果它是在db ofcourse中找到的(如果我指向site.com/harvey的鏈接,php應該在db中找到Harvey並顯示例如他的電話,地址和其他詳細信息,但是這不能預加載,但只能請求點擊由於帶寬問題)。

  • 史:我想有在網站上的選項,其中向後和向前運動將完美與動畫工作,因爲它的工作原理,同時點擊鏈接..

  • 多變的div裏面的工作聯繫:因爲有是一些腳本不會改變內容鏈接的內容的問題(例如第一個內容框包括導航,應該用第二個框中的不同導航來替換),再次使用github這個工作。

我希望你給我一些鏈接,甚至更多的幫助p有一些代碼..

+0

更好地爲您遇到的特定問題撰寫具體問題。 – dynamic 2012-02-13 15:14:16

回答

1

我認爲你正在尋找的腳本是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 
} 
?> 
+0

偉大的思想,呃? – JKirchartz 2012-02-13 15:17:55

+0

確實:) 它完全符合他的問題.. – ramsesoriginal 2012-02-13 15:29:03

+0

它是一個偉大的腳本,即使有IE兼容性的一些問題,..但是,我不喜歡關於pjax的是,它基於Ruby和我想在PHP中有一些東西.. – 2012-02-14 14:52:35

0

如果你想要的網址,當你阿賈克斯頁面請求,您可以使用顯示的「正確」的網址PJAX它改變了歷史與pushState但它不工作ie6-ie9檢查出CanIUse: History兼容性問題。

相關問題