Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
Shopify app theme extensions jQuery not loading/not working

I'm trying to create a theme app extension for shopify to run some javascript when the add to cart button is clicked. As an example:

<head>
  <script type="text/javascript" src={{ 'jquery-3.6.0.js' | asset_url }} defer></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" rel="preload" defer="defer"></script>

</head>
...
<button id="add-btn">click me!</button>

<script>
  jQuery( document ).ready(function() {

    $('#add-btn').on('click', function(event){
      alert("clicked")
    })
  });
</script>

This is within a sheet app extension in the blocks folder.

It seems like jquery isn't loading, and I get an error

 Uncaught ReferenceError: jQuery is not defined

Any ideas why?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Summary: You are loading the jQuery library using the defer keyword so that it doesn't block the page load, but are referencing jQuery before the page finishes loading.


By loading jQuery using the defer attribute on the script tag, the browser will start loading the script immediately but won't execute the script until the DOM content has loaded:

<script type="text/javascript" src={{ 'jquery-3.6.0.js' | asset_url }} defer></script>

However, the inline script tag further down the page will be run immediately when the browser gets to it:

<script>
  jQuery( document ).ready(function() {  // ERROR!  jQuery library hasn't been run yet, so we can't use jQuery to select the document!

    $('#add-btn').on('click', function(event){
      alert("clicked")
    })
  });
</script>

Since deferred scripts are executed in the order that they are defined, your instinct might be to try putting the defer keyword on the inline script tag - but unfortunately, defer and async only apply to external script files.

There are several ways that we could resolve this timing issue. Two possibilities are:

1: Move the inline script to an external Javascript file

If you create an external JS file, you can load the script using the defer keyword as all deferred scripts execute in the order that they were called once the DOM is ready. As an added bonus, you wouldn't need to wrap your function with the jQuery(document).ready as you would already know that the document is ready when the script executes.

2: Use vanilla Javascript rather than jQuery to set up your event listener

If the inline script tag makes sense, then using vanilla Javascript to set up your event will solve your issue. The equivalent code would be to use the addEventListener function to listen for the DOMContentLoaded event:

<script>
  // This event is created before the DOM is loaded, so jQuery doesn't exist yet
  document.addEventListener('DOMContentLoaded', function() {

    // This event will be run after the deferred scripts above, so jQuery exists now
    $('#add-btn').on('click', function(event){
      alert("clicked")
    })
  });
</script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda