Looking for a way to retrieve data using AJAX without an onclick function? Most examples I found use the onclick function. Here is an HTML example where I successfully retrieved data using an onclick function.
<!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)">
click here
</aside>
</body>
here is the script
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;
}
Now, instead of using the onclick function, I want the data to be displayed when the HTML page is opened. Can someone guide me on how to achieve this?