Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Async AJAX with Script Response

I'm working on a live ajax search like Google. But I have problems to understand the async behaviour.

I have two js scripts: Script1.js calls a php script and the response is html which includes another script2.js. Script2 is a jquery-plugin. But every time I call my jquery-plugin in my php response

$('body').my_plugin(); 

I get an error in the console, that this plugin doesn't exist.

Here's my example:

script1.js makes an asynchronous AJAX call like so:

 $.ajax({
    type: 'GET',
    url: 'my_url/search.php',                   
 }).done(function(response) {
    $('#response-div').empty().append(response);
      ...
 }).fail(function() {
      ...
 });

The php html-response looks like so:

...

<script type="text/javascript" src="js/script2.js"></script>

<script type="text/javascript">     
$(document).ready(function() {    
    $('preview-div').my_plugin();
});
    
....

When I make an synchronous AJAX call it works. When I use the getScript jQuery function in my php response it works too:

 <script type="text/javascript">     
    $(document).ready(function() {  

       $.getScript('js/sript2.js', function(jd) {
         $('preview-div').my_plugin();
       })

    });

Can someone explain the difference to me please? What is best practice for loading asynchronous content which includes it's own js-scripts?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!