Comprobando si jquery está cargado usando Javascript

Resuelto SoftwareSavant asked hace 13 años • 12 respuestas

Estoy intentando comprobar si mi biblioteca Jquery está cargada en mi página HTML. Estoy comprobando si funciona, pero algo no está bien. Esto es lo que tengo:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="/query-1.6.3.min.js"></script>
        <script type="text/javascript">
          $(document).ready(function(){
             if (jQuery) {  
               // jQuery is loaded  
               alert("Yeah!");
             } else {
               // jQuery is not loaded
               alert("Doesn't Work");
             }
          });
        </script>
SoftwareSavant avatar Sep 08 '11 07:09 SoftwareSavant
Aceptado

algo no está bien

Bueno, estás usando jQuery para comprobar la presencia de jQuery. Si jQuery no está cargado, $()ni siquiera se ejecutará y su devolución de llamada no se ejecutará, a menos que esté usando otra biblioteca y esa biblioteca comparta la misma $()sintaxis.

Elimina tu $(document).ready()(usa algo como window.onloaden su lugar):

window.onload = function() {
    if (window.jQuery) {  
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}
BoltClock avatar Sep 08 '2011 00:09 BoltClock

Según este enlace:

if (typeof jQuery == 'undefined') {
    // jQuery IS NOT loaded, do stuff here.
}

Hay algunos más en los comentarios del enlace, así como,
if (typeof jQuery == 'function') {...}

//or

if (typeof $ == 'function') {...}

// or

if (jQuery) {
    console.log("jquery is loaded");
} else {
    console.log("Not loaded");
}

¡Espero que esto cubra la mayoría de las buenas formas de hacer esto!
Tushar Shukla avatar Apr 20 '2016 06:04 Tushar Shukla
if ('undefined' == typeof window.jQuery) {
    // jQuery not present
} else {
    // jQuery present
}
Niko avatar Sep 08 '2011 00:09 Niko

Puede hacer esto rápidamente en la pestaña de la consola al inspeccionar su página web.

P.ej:

$ === jQuery

Si regresa truesignifica que está cargado.

Alexein avatar Mar 15 '2018 20:03 Alexein