0

I am working with a filter on page to display different products. I am trying to make it so that when you click a button from an external page, it navigates to the product page and automatically clicks the "organics filter" checkbox and then displays the organic items. This code works in every browser except IE and EDGE. Any ideas what could be wrong? It appears to click the organics checkbox but it does not actually click the submit button for IE and EDGE.

**Update: It works if I put an alert after the window.load function. I think its a timing issue or something. Anyone have any suggestions?

//force organic filter if coming from organics article
    $(window).load(function(){
    var organicURL = document.referrer;
    if (organicURL === "http://www.sampleurl.com") {
       $('#organicFilter').trigger('click');
       if ($('#organicFilter').is(':checked')) {
          $('#submitFilter').trigger('click');
       }
    }
    });
Tom
  • 305
  • 1
  • 3
  • 15
  • Possible duplicate of [IE did not set document.referrer](http://stackoverflow.com/questions/13681156/ie-did-not-set-document-referrer) – gaetanoM Mar 31 '17 at 14:23

2 Answers2

0

In order to trigger click event, you should use the jQuery .click() function. Also, have a look here : jQuery.trigger('click') not working and here : Trigger click event not working in IE10

Community
  • 1
  • 1
geo
  • 2,283
  • 5
  • 28
  • 46
  • thank you. It seems to work if i put an alert first. Its a timing issue i think. Any way I can resolve this? – Tom Mar 31 '17 at 14:43
0

I fixed it by adding this below:

$(window).load(function(){
var organicURL = document.referrer;
if (organicURL === "http://www.centowine.com/articles/2017/cento_organics.php") {
   $('#organicFilter').trigger('click');
   if ($('#organicFilter').is(':checked')) {
     setTimeout(function(){ $('#submitFilter').click()}, 500);
   }  
}
});

Just needed to add a timeout it was loading too soon. Before the document loaded.

Tom
  • 305
  • 1
  • 3
  • 15
  • Adding timeout in order to run your code is not a proper solution. This is similar to take nullpointer exception and fixing it by adding an if statement (if the variable is null do this). – geo Mar 31 '17 at 14:50
  • @geo what do you recommend? – Tom Mar 31 '17 at 14:52
  • If the issue is the "time" means that you do an asynchronous call and you don't know when your promise will be resolved. If this occurs, you could create a promise and run your code after the resolution of your promise. – geo Mar 31 '17 at 14:52