Why can't I open a new window in my jquery ajax callback?

to show my problem in a few examples ...

THIS IS WORKS

$.post("SomePage.aspx", { name : "value" },
      function (data) {
         alert(data);
      }, "text");

      

THIS DOES NOT WORK

$.post("SomePage.aspx", { name : "value" },
          function (data) {
             window.open("http://www.google.com");
          }, "text");

      

In the first example, I get an alert with what I expect. In the second example, nothing happens. The window does not open. If I add an alert or something before or after calling window.open, the alert works fine, but the window doesn't open. If I add window.open after the $ .post method, the window opens fine (of course it won't help me at all).

I am wondering why I am not able to open the window in the callback. What do I need to do to open the window? I would like to open a window to show some fancy results.

Any help is appreciated, thanks.

+2


a source to share


2 answers


must work?

try it with window name?

window.open("http://www.google.com", "MyWindow");

      

according to this post

http://www.javascript-coder.com/window-popup/javascript-window-open.phtml



The syntax for the window.open method is as follows: open (URL, windowName [, windowFeatures])

the second parameter is not optional (of course everything is optional in javascript), maybe something weird is happening in the callback without it?

is it possible to open it in a new tab or behind the current window?

var x = window.open("http://www.google.com", "MyWindow");
x.focus();

      

+3


a source


This is my test solution:

$.ajax({
    type: "GET",
    async: false,
    . . .
    url: "MyService.aspx/ConstructUrl",
    success: function(url) {
        window.open(url); 
})

      



added extra ajax option "async: false" and this caused the url to open in a new tab. It works well in chrome and firefox.

+2


a source







All Articles