开发者

close existing opened window inside XMLHttpRequest handler

开发者 https://www.devze.com 2023-03-31 07:20 出处:网络
Currently I\'m using Javascript with Ajax to fetch some data and present it in a new window.I\'m trying to close the window in OpenFileWindow() before a new one is opened, but while the it finds the w

Currently I'm using Javascript with Ajax to fetch some data and present it in a new window. I'm trying to close the window in OpenFileWindow() before a new one is opened, but while the it finds the window object, all properties and methods give a permission denied error.

I'm believe it has to do with scoping with the Ajax call as when I open the window before XMLHttpRequest, there's no problem.

I'm not sure how to proceed and I've searched quite a bit. Anyone have any suggestions? Thanks.

opened

var newWin = null;
function View_onClick(propId, url) {
  开发者_开发知识库  var param = "propId=" + propId;
    param += "&filename=" + url;
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "GetActivityFileName.ashx", false);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                if (xhr.responseText == "") {
                    alert("Sorry unable to find file.");
                    return false;
                }
                else {
                    OpenFileWindow(xhr.responseText)
                    return false;
                }
            }
        }
    }
    xhr.send(param);
    return false;
}


function OpenFileWindow(fileUrl) {
        if(newWin != null)
            newWin.close();
        newWin = window.open(fileUrl);
        newWin.focus();
}


If your intention is to just re-use the window, why not name it.

If you give the same name as the 2nd parameter of window.open, it will re-use that window.


How about this. If the window is still open, change the URL. Otherwise, open the window and load the URL.

function OpenFileWindow(fileUrl) {
        if(newWin == null || newWin.closed) 
          newWin = window.open(fileUrl);
        else
          newWin.location.replace(fileUrl);

        newWin.focus();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消