Pregunta

I want this script to load only on homepage, at the moment it is opening in all the pages on my Shopify store. I am using QuickShop Theme. Below is the code, this code is for my newsletter popup. Any help would be appreciated. Thanks a ton in advance.

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

var $_form = $('#mc-form');
var $_action = $_form.attr('action');
$_form.ajaxChimp({
  url: $_action,
  callback: function(resp){
    //
  }
});    
});

$(window).load(function() {

{% if settings.popup_expires == '0' %}
$.removeCookie('popup_cookie');
{% endif %}

var $_popup_cookie = $.cookie('popup_cookie');
if ($_popup_cookie == undefined){
  setTimeout(function(){

    $('#mc-form-fancybox').fancybox({
      'beforeClose': function(){
        if($("#mc-popup-hide").is(':checked')){
          $.cookie('popup_cookie', 'yes', { expires: {{settings.popup_expires}} });
        }
        else{
          // no set cookie
        }

      }
    }).trigger('click');
  }, 1000);
}

});
</script>
¿Fue útil?

Solución

Instead of putting your script in theme.liquid, try putting it in index.liquid instead. theme.liquid is rendered for every page on your site, but index.liquid is your homepage template.

Otros consejos

There are many possible ways to do this. You could only include the script on your home page, that way it would only fire on the page its getting loaded on. If you can't do this you can always interrogate the window.location property to work out what page you are.

Try putting window.location into the JavaScript console of your browser and see what comes back. You'll get properties such as host (e.g. stackoverflow.com), href (e.g. Limiting Pop to load on homepage- Shopify), pathname (e.g /questions/23427460/limiting-pop-to-load-on-homepage-shopify) and more.

Using this you can work out if you are on the homepage or not and not fire your fancybox.

There are many other possibilities as well, that's just off the top of my head. I would recommend just making sure it is included in just your home page if you can, but if not the window.location property may help.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top