2017-04-07 1 views
1

私はonclick関数なしでajaxを使用してデータを取得する例を探していましたが、
はここでここユーザーajaxを使用してデータを取得し、 "onClick function"をJavaScriptで取得しない

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の機能なしでデータを取得したかったスクリプトである私が成功の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です。
一度htmlを開くとデータが表示されます。
誰かが私にいくつかのリンクを提供することはできますか?

+0

なぜonclickから、

1

あなたがする必要があるのは、たとえば、ウィンドウがロードされたときに呼び出されるように設定するなど、他の場所で関数を呼び出すことだけです。

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

1

あなたはページをレンダリングブラウザ後にクエリを送信するために新しいDOMContentLoadedイベントを追加することができ

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

1

第三の溶液を使用することができます。必要な

document.addEventListener('DOMContentLoaded', loadDoc);

1

JS

でウィンドウのonload関数を使用することができます。 だから、コードの下 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

にonclickのは、理想的には同じドメインのページあれば動作します交換してください。

<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 = 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

<!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イベントであなたの関数を呼び出します

関連する問題