Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda