0

I have the following JavaScript code:

dropdown.onchange  = function(e){
    e.preventDefault();
    var id = this.value;
    var w = window.open('', '_blank');

    $.ajax({ 
        url: '../../getSearchSonBySearchId.php',  
        type: 'POST',
        async: false,
        dataType: 'Text',
        data: {'search_id': id},
        error: function(a, b, c) { alert(a+b+c); }      
    }).done(function(data) {
        var search_criteria = data; 
        var cx = '*******';
        var api_key = '******';
        w.location = "http://www.calimedia.net/search.html?q=" + search_criteria;                       
    });
};

In the code above, imagine dropdown is a variable referring to an HTML drop down list. I am trying to avoid browser popup blockers when I select an option from my drop down list.

I have checked the following questions in SO and a few others:

It looks like the only way of preventing a popup is if the event of opening a new page comes from a trusted event or from a user (person). Is there any way I can make the browser think my onchange event is a trusted event?

Community
  • 1
  • 1
Erick
  • 823
  • 16
  • 37
  • If the `change` event is triggered by a user action, it should be a trusted event. So the popup blocker shouldn't interfere. – Barmar Feb 12 '16 at 03:07
  • How do I know if the change event is a triggered by a user action? I mean, the user selects an option from a drop down list and in the back end it goes through that change event. The user triggered it. But in this case, the popup blocker is interfering. – Erick Feb 12 '16 at 03:22
  • If the user selects the option from the dropdown, that's a user action. Are you using a plugin that replaces the real dropdown with HTML elements, like `Select2`? – Barmar Feb 12 '16 at 03:24
  • No but now that you mention that, I am creating the select element using JavaScript: document.createElement("select"). Would that matter? – Erick Feb 12 '16 at 03:28
  • No, it shouldn't. All that matters is whether the event happened as a direct result of user interaction, or it was triggered by the program. – Barmar Feb 12 '16 at 03:29
  • Ok. Just in case it matters, inside my onchange function I am doing an asynchronous AJAX call to retrieve some data from the server to user to send a GET request. Not sure if that matters either. Not sure what is going on since the event happens as a direct result of user interaction – Erick Feb 12 '16 at 03:31
  • Yes, that matters. If you're opening the window in the callback function, that's no longer linked to the event. It's asynchronous, and there's no way for the browser to know that it's related to the user action. – Barmar Feb 12 '16 at 03:37
  • Is there any possible way to solve this? What are my alternatives here? – Erick Feb 12 '16 at 03:39
  • See the question I just linked to above. Basically, you open an empty window in the `onchange` handler, and in the AJAX callback you set the window's location to the URL. – Barmar Feb 12 '16 at 03:41
  • Thanks Barmar but I had tried that before. I updated the question with the updated code following the answer from the question you posted. What am I doing wrong? – Erick Feb 12 '16 at 03:47
  • That's nothing like the answers in the other question. You have to do `var w = window.open('', '_blank')` **before** you call `$.ajax`. Then in the `.done()` function you do `w.location = "http://www.calimedia.net/search.html?q=" + search_criteria;`. – Barmar Feb 12 '16 at 03:53
  • For some reason the browser still keeps blocking the popup. I updated the code in the question with your suggestion – Erick Feb 12 '16 at 04:04
  • I don't know why. Does it block the popup if you don't perform the AJAX call? – Barmar Feb 12 '16 at 04:18
  • @Barmar I removed the AJAX call and it still blocks the popup. Basically I have my code as it is posted on the question (no AJAX call) – Erick Feb 15 '16 at 21:45
  • Very strange. Popups are supposed to be allowed in the `onXXX` function. Try disabling all your browser extensions, in case one of them is interfering. – Barmar Feb 15 '16 at 22:33

0 Answers0