Why can't I open a new window on my jquery aja

2020-07-27 03:50发布

to show my problem in a couple examples...

THIS WORKS

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

THIS DOESN'T WORK

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

In the first example, I get alerted with what i'm expecting. In the second example, nothing happens. No window is opened. If I add an alert or something before or after the window.open call, the alert works fine, but the window doesn't open. If I add a window.open completly after the $.post method, the window opens fine (of course this doens't help me at all).

I'm wondering why I can't open a window in the callback. What do I have to do to be able to open a window? I'd like to open a window to show some fancy results.

Any help is appreciated, thanks.

2条回答
家丑人穷心不美
2楼-- · 2020-07-27 04:37

should work?

try it with the name of the window?

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

according to this post

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

The syntax of the window.open method is given below: open (URL, windowName[, windowFeatures])

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

also, possible it is opening in a new tab or behind the current window?

var x = window.open("http://www.google.com", "MyWindow");
x.focus();
查看更多
等我变得足够好
3楼-- · 2020-07-27 04:39

This is my sample solution:

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

added additional ajax option "async:false" and it caused openning URL in a new Tab. It works well in chrome and firefox.

查看更多
登录 后发表回答