1

I've been searching for the example of getting data by using ajax without onclick function but most of the example that i found was within onclick funtion.
Here is the html that i successfully get the data by using Onclick funtion

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

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, i wanted get the data without the onclick function.
I want the data show once i open the html.
Can someone provide me some link or gv me an example ??

3
  • Why don't you move the function call out of onclick and within <script> tags? That should resolve it. Commented Apr 7, 2017 at 6:35
  • you have the loadDoc function which is independed to onClick, you may call it from wherever you want. Perhaps you want to ask how to call a function when the page loads? Commented Apr 7, 2017 at 6:35
  • Thanks for every suggestion and answer! i've learnt a new things today! =) Much Appreciate! Commented Apr 7, 2017 at 7:16

7 Answers 7

1

Body onload doesnt required. You can directly use window onload function in JS

JS

window.onload = function() {
  loadDoc('getData.php',myFunction)
};
Sign up to request clarification or add additional context in comments.

2 Comments

He needs to remove the onclick event over which the function is loading if im not wrong.
That was the question.. :)
1

All you need to do is call the function elsewhere for instance you could set it to be called when the window is loaded.

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

Comments

1

You can use

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

Comments

1

A third solution could be to add a new DOMContentLoaded event to send the query after the browser render the page.

It should be something like this :

document.addEventListener('DOMContentLoaded', loadDoc);

Comments

1

You have bound ajax call to an event onclick where what you want is at page load. So replace the onclick to 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>

Comments

1

below code will ideally work if the page of same domain.

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

Comments

1

Call your function in window.onload event instead of 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>

here is the script

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;

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.