3

I have the following line of code that is called using jQuery when a link is clicked:

  alipayTransactionModalTrigger.click(function(e) {  
     e.preventDefault();             
     $.ajax({
        url: "doSomething",
        data: "p_locale=en-US",
        success: function(dataOut){
           if (dataOut == "hold") {
              ...do something...
           } else {   
              //open a modal over parent window
              alipayTransactionModal.dialog("open"); 
              //also open new tab/window
              var Payment = window.open("http://www.alipay.com", "Payment");                  
              Payment;                
              Payment.focus();
           }
        }
     });    
     return false;
  });

The ajax call just checks some values basically. The success call is happening like it should, that is not the problem. Through the use of the frame name I am trying to force the newly opened window/tab to get refreshed when the link is clicked more than once. This works in IE 8, Chrome, and Firefox - a user can click the link any number of times, and it will only open a new window once - subsequent clicks just reload that window.

However, this is not happening with IE 9 - IE 9 will open a new window/tab each time. Any way to force this behavior in IE 9? Or even just an explanation of why this is occurring in IE 9 would be great - is it a user level setting? From what I can tell, IE9 just does not know what I am pointing it towards when I use the frame name in window.open().

Another thing I should mention - the .focus() does not always focus the window on second/third/etc clicks (seems to depend on browser and browser settings) - but I have already asked that question here Just including it here in case it is relevant.

Thanks

Community
  • 1
  • 1
mkohanek
  • 69
  • 1
  • 10
  • Additionally, keep a reference to the window in a variable: `var w = window.open(....); .... w.focus();` – Christian Feb 11 '13 at 22:35
  • Are you saying this is better practice than using `.focus()` the way I am now? Or that this should solve my problem? This seems to make no difference in reference to new windows opening in IE 9 or not. – mkohanek Feb 11 '13 at 22:39
  • I'm saying when the link is clicked the second time, user the variable `w` instead of opening a new window. Should be easy to implement and works across all browsers. – Christian Feb 11 '13 at 22:57
  • Are you really using "www.test.com" instead of the proper http://-prefixed url? Is the new window same-origin to the original window? – EricLaw Feb 11 '13 at 22:58
  • Christian - I have tried that as well, with the same results EricLaw - no, that is not the actual url I use - the url gets generated elsewhere so I just inserted that for the sake of simplicity – mkohanek Feb 13 '13 at 16:35
  • Christian - sorry, I mis-spoke in the comment above - I have tried what you are saying to use, but not exactly with similar results. See the comment to the 1st answer given below to get a detailed explanation of what happened with similar code. – mkohanek Feb 13 '13 at 17:18

2 Answers2

1

opening a window with window.open from a ajax callback is doomed to be blocked by the popup blocker. i believe if you disable it it will work as expected.

Check this answer right here. Window.open blocked You need the right context for your window.

Community
  • 1
  • 1
TheBrain
  • 5,528
  • 2
  • 25
  • 26
  • Thanks The Brain, I was unaware this was an issue - I have implemented the suggestion to [make the call synchronous](http://stackoverflow.com/questions/6628949/window-open-popup-getting-blocked-during-click-event) - so should that bypass the pop up blocker warnings I had seen before? (I have already set to always allow so I don't see them anymore). I would upvote this answer but I am not yet at 15 rep so I cannot. Still, the original problem remains, IE9 always opens a new window, and does not refresh the original window. Code updated above to reflect changes – mkohanek Feb 13 '13 at 17:59
0

I have no IE9 for testing but maybe this helps

var openLink = (function() {
    var _popups = {};

    return function(link, name) {
        if (_popups.hasOwnProperty(name) === false) {
            _popups[name] = window.open();
        }

        _popups[name].location.href = link;
        _popups[name].focus();
    };
}());

Example

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • Ah, you know, as I tried to implement this, I realized that I probably need to add more context to this. This is all happening within an ajax jquery function call (I will edit the original post to show more context). Also, in between clicks that open the new window, the parent window gets reloaded - so the var you set here gets reset each time, and so `_popups.hasOwnProperty(name) === false` is always true. However, even without page refresh, the new window just did not reload at all in IE 9 using your suggestion -it opened the first time, but subsequent clicks did not reload the new window – mkohanek Feb 13 '13 at 17:09
  • So for some reason IE9 just does not seem to know what I am trying to point it to when I use the frame name. I have to think this is an issue with IE9 since it does not occur in FF, Chrome, or even IE8 – mkohanek Feb 13 '13 at 17:21