0

第一次来这里。我正在尝试使用 Javascript 编写具有这些功能的代码。

首先,如果单击链接,并且没有现有的弹出窗口,则会创建一个弹出窗口,导航到该链接。

但是,如果单击第二个链接,并且有一个现有的弹出窗口,它将运行一些 AJAX 功能。我们不会导航到该链接。

但是,如果单击另一个链接,并且弹出窗口已关闭,它将再次打开该窗口(并导航到该链接)。

我能想到的唯一让我解决这个问题的方法是使用全局变量,但是它没有解决。有人可以帮忙吗?谢谢!

这是我的jsfiddle

的HTML

<a href="javascript:;" onclick="javascript:displayWindow=openwindowPreview(1, displayWindow); return false">1</a>
<br/>
<a href="javascript:;" onclick="javascript:displayWindow=openwindowPreview(4, displayWindow); return false">4</a>

Javascript

var displayWindow = null;
var test = 'test';

function openwindowPreview(id, winObject) {
    // check if the window already exists
    if (winObject != null) {
        // the window has already been created, but did the user close it?
        // if so, then reopen it. Otherwise make it the active window.
        if (!winObject.closed) {
            winObject.focus();
            return winObject;
        }
    }

    if (test != 'test') {
        if (winObject.closed) {
            test = 'test';
        } else {
            alert('ajax');
        }
    }

    // if we get here, then the window hasn't been created yet, or it
    // was closed by the user.
    if (test == 'test') {
        var urlDisplayID= "file.php?ID=" + id;
        window.open(urlDisplayID, 'width=' + screen.width, 'height=' + screen.height);
        test = 'tested';
    }
}

基本上,一次只允许一个窗口实例,并且只显示第一个实例,而其他实例(不同的 URL — 由于参数)通过 AJAX 发送到服务器。

4

1 回答 1

0

在你的情况下,我认为这可能是你的需要。

var openWindows = {};

function openwindowPreview(id) {

// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
var urlDisplayID= "file.php?ID=" + id;
//set id as the window name, so if the window already opened,
//the open method will find the opened window with the window name
var opened =  window.open(urlDisplayID, id,'width=' + screen.width, 'height=' + screen.height);
if(opened === openWindows[id]){
    alert("ajax");
}else{
    openWindows[id] = opened();
}

}

于 2013-04-24T05:31:37.777 回答