Tengo un archivo Javascript externo initialize_database.js que usa JQuery para llamar a un script PHP para crear una base de datos y algunas tablas. Probé mi script PHP añadiéndole algo de HTML para ejecutarlo por sí solo, y funciona bien. Mi HTML es el siguiente:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <title>Test Webpage</title> </head> <body> <script src="initialize_database.js"></script> <div> <h2>Nothing here yet!</h2> </div> </body> </html> Aquí está initialize_database.js :
$(document).ready(function() { $.get('testPhp.php' { alert('Databases were initialized'); }); });Me gustaría que Javascript se ejecute tan pronto como se cargue la página para que la base de datos se pueda crear de inmediato. Todos los archivos están en el mismo directorio. ¿Qué estoy haciendo mal?
Se requiere la inicialización de función y coma después de la URL para ejecutar la función correctamente.
$(document).ready(function() { $.get('testPhp.php', function() { alert('Databases were initialized'); }); });Mira esto
Puedes usar este código. si quieres correr cuando dom esté listo, entonces
$(document).ready(function() { $.get('testPhp.php', function() { alert('Databases were initialized'); }); }); if you want to run this full load then use bellow $( window ).load(function() { $.get('testPhp.php', function() { alert('Databases were initialized'); }); });