Tom
Tom

Reputation: 345

Trying to use a data tag and click event to trigger a function

I am working on a page and I am trying to reload the page on click and execute a function. This code works on Safara and Firefox, but it doesnt work on Chrome and im not sure about IE. Hoping someone can help me. Below is my code. Not sure if there is a better way of doing this.

Basically i am just create a URL and on click passing a data tag and then grabbing the data tag and assigning the category to the end of a url. Then i am executing the category url function that is called "#tomatoes". I have to reload the page because the function is already called earlier.

var categoryTitle = 'Tomatoes';
$('.activeCategory').html('<a href="#" class="activeCategory" data-caturl="#tomatoes">' + categoryTitle + '</a>');

//change the category dynamically if clicked from individual product page
      $('.activeCategory').click(function(event) {
          var caturl = $(this).children('a').data('caturl');
          var newWindow = window.location.href="http://www.example.com/DEV/product-categories/example.php" + caturl;
          if (newWindow === "http://www.example.com/DEV/product-categories/example.php" + caturl) {
              location.reload();
              $("caturl").click();
          } 
      });

Upvotes: 1

Views: 87

Answers (1)

Tumen_t
Tumen_t

Reputation: 801

I would reload the page and add hashtag # and then execute your function. There is no way to have a callback on page reload, because your JS application gets re-initialized and starts all over again.

var categoryTitle = 'Tomatoes';
$('.activeCategory').html('<a href="#" class="activeCategory" data-caturl="#tomatoes">' + categoryTitle + '</a>');

//change the category dynamically if clicked from individual product page
      $('.activeCategory').click(function(event) {
          var caturl = $(this).children('a').data('caturl');
          var newWindow = window.location.href="http://www.example.com/DEV/product-categories/example.php" + caturl;
          if (newWindow === "http://www.example.com/DEV/product-categories/example.php" + caturl) {
              if(window.location.hash) {
                $("caturl").click();
              } else {
                 window.location = window.location.href + "#refresh";
                 location.reload();
              }



          } 
      });

Upvotes: 1

Related Questions