我有一个列出上传附件的页面,每个附件旁边都有一个图标,用于打开弹出窗口(单击时),以允许用户编辑每个附件的各种附件信息。
我的问题是:
如何确保一次只允许用户打开一个弹出窗口
当用户尝试打开另一个弹出窗口时,会弹出一个警告并告诉他/她不允许他们打开多个窗口。
答案 0 :(得分:3)
打开时为弹出窗口命名。这将强制在同一弹出窗口中打开任何后续点击。然后,您可以实现window.unload事件,以防止用户导航离开当前正在保存的页面。
如果您使用的是jQuery,我建议您使用ui.dialog在页面中提供功能。然后你还会强制用户一次一个地编辑。
答案 1 :(得分:3)
创建新弹出窗口时,将变量设置为指向新窗口,然后检查它是否已关闭:
var currentPopup = null;
function openPopup(){
//test that we have either not opened a popup or have closed it.
//references to closed windows don't disapear, but have their closed property set to true.
if(!currentPopup || currentPopup.closed){
//set the new currentPopup variable to reference the new window.
currentPopup = window.open(...);
}
else{
//A window is open! Display error message.
//for the best user experience use a nice way to display the message as opposed to using an alert();
}
}
答案 2 :(得分:2)
您可以使用窗口的“手柄”并检查它是否打开,以下是示例:
var vWin = null;
var msWindowUsedMessage = "A previous action was not completed.\nClose the window and try again.";
function IsWindowUsed(oWindowToCheck) {
if (!oWindowToCheck.closed && oWindowToCheck.location) {
alert(msWindowUsedMessage);
oWindowToCheck.focus();
return true;
}
return false;
}
function Open_New_Window(sUrl, sTitle, sParams) {
var winLeft = top.screenLeft + 50;
var winTop = top.screenTop + 50;
var oWindowHandle = window.open(sUrl, "", sParams + ",toolbar=no,top=" + winTop + ",left=" + winLeft);
oWindowHandle.opener = self;
return oWindowHandle;
}
function Add() {
if (IsWindowUsed(vWin)){
return;
}
var sParams = "width=380,height=10,status=no,resizable=yes";
var sUrl = "add.aspx";
vWin = Open_New_Window(sUrl, 'Add', sParams);
}
答案 3 :(得分:0)
为什么不制作每个弹出模式,以便在与主页交互之前必须将其解除。使用jQuery Dialog plugin这很容易,但你也可以用一些javascript / css魔法制作自己的。
答案 4 :(得分:0)
根据您的需要创建一个cookie或全局(客户端或服务器端)标志,并检查每次触发弹出窗口时是否设置它。