2017-04-07 94 views
1

我一直在尋找通過使用沒有onclick函數的ajax獲取數據的例子,但是我發現的大部分例子都在onclick函數中。
這裏是我成功使用的onclick funtion用戶ajax在javaScript中沒有「onClick函數」的情況下獲取數據

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>New document</title> 

    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 

<script type ='text/javascript' src='js/testScript.js'></script> 

<?php 
include_once("database_conn_getOffers.php"); 
?> 
</head> 
<body> 
    content goes here 
    <aside id="offer" onclick ="loadDoc('getData.php',myFunction)"> 
     &nbsp; click here 
     </aside> 
</body> 

獲取數據的HTML這裏是腳本

function loadDoc(url, cFunction) { 
    var xhttp; 
    xhttp=new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     cFunction(this); 
    } 
    }; 
    xhttp.open("GET", url, true); 
    xhttp.send(); 
} 
function myFunction(xhttp) { 
    document.getElementById("offer").innerHTML = 
    xhttp.responseText; 
} 

現在,我想獲得的數據沒有的onclick功能。
我希望數據顯示一旦我打開html。
有人可以提供一些鏈接或gv我的例子?

+0

爲什麼不將函數調用移出onclick並在

1

所有你需要做的就是調用函數的其他地方,例如你可以將它設置爲在窗口加載時調用。

Window.onload = loadDoc('getData.php',myFunction)

1

您可以使用

<body onload="loadDoc('getData.php',myFunction);">

1

第三種解決辦法是增加一個新的DOMContentLoaded事件來發送瀏覽器後呈現頁面查詢。

應該是這樣的:需要

document.addEventListener('DOMContentLoaded', loadDoc);

1

已經綁定Ajax調用的事件onclick,你想要的是在頁面加載。 所以更換的onclick到onload

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>New document</title> 

    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 

    <script type ='text/javascript' src='js/testScript.js'></script> 

<?php 
include_once("database_conn_getOffers.php"); 
?> 
</head> 
<body> 
    content goes here 
    <aside id="offer" onload ="loadDoc('getData.php',myFunction)"> 
    &nbsp; click here 
    </aside> 
</body> 

1

下面的代碼將理想的工作,如果同一個域的頁面。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>New document</title> 
 

 
    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 
 

 
<script type ='text/javascript' src='js/testScript.js'></script> 
 
<script type ='text/javascript'> 
 
$(document).ready(function(){ 
 
$("#offer").load('getData.php'); 
 
}); 
 
</script> 
 

 
<?php 
 
include_once("database_conn_getOffers.php"); 
 
?> 
 
</head> 
 
<body> 
 
    content goes here 
 
    <aside id="offer" > 
 
     &nbsp; click here 
 
     </aside> 
 
</body>

1

window.onload事件中調用你的函數,而不是onclick

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>New document</title> 

    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 

<script type ='text/javascript' src='js/testScript.js'></script> 

<?php 
include_once("database_conn_getOffers.php"); 
?> 
</head> 
<body> 
    content goes here 
    <aside id="offer"> 
     Your function has been executed onload of document 
     </aside> 
</body> 

這裏是腳本

window.onload = function loadDoc(url, cFunction) { 
    var xhttp; 
    xhttp=new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     cFunction(this); 
    } 
    }; 
    xhttp.open("GET", url, true); 
    xhttp.send(); 
} 
function myFunction(xhttp) { 
    document.getElementById("offer").innerHTML = 
    xhttp.responseText; 

}; 
相關問題