[英]Auto-refreshing content (using template engine) using jQuery
我正在尋找一種將jQuery與腳本index.php結合使用的方法,該腳本是使用模板引擎逐位構建的,如下所示:
<?php
global $template;
page_header(); // Calls and displays header.html
$template->load('body', 'index.html'); // Loads index.html and calls it 'body'
*Code that assigns values to variables in index.html*
$template->display('body'); // Displays index.html
page_footer(); // Calls and displays footer.html
?>
每行所做的評論。
我想使用自動刷新index.php的jQuery構建函數,但僅刷新“ body”部分。 我找不到有關此操作的任何文檔,但是我查看了以下代碼:
function update() {
$("#notice_div").html('Loading..');
$.ajax({
type: 'GET',
url: 'index.php',
timeout: 2000,
success: function(data) {
$("#current_game").html(data);
$("#notice_div").html('');
window.setTimeout(update, 10000);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#notice_div").html('Timeout contacting server..');
window.setTimeout(update, 60000);
}
});
};
但是當它放在標題中時,它會變得非常混亂,因為它調用了嵌套標題的整個index.php頁面,因此我們在標題中有一個標題。 有什么建議么?
最簡單的解決方案是發送get變量;
function update() {
$("#notice_div").html('Loading..');
$.ajax({
type: 'GET',
url: 'index.php',
data: 'ajax=true',
...
}
並在您的php文件中執行以下操作:
if($_GET['ajax'] == 'true') {
// don't print header, but other stuff
} else {
// print header
}
如果您使用的是框架,則它可能具有一些智能功能來自動確定它是xhttp請求還是普通請求。 通常通過檢查標題來完成
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.