我有一个附加到名为“navigate.js”的网页的 JavaScript 文件,它包含一组如下所示的方法:
var ContractWindow;
function showContract(ContractID){
try{
if(ContractWindow){
if(!ContractWindow.closed){
ContractWindow.close;
}
}
ContractWindow = window.open(contract.aspx?contractid=" + ContractID, "ContractEditor",
"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=780,height=615");
}
}catch(exc){
alert(exc.message);
}
}
(有类似打开页面编辑商品分类、商品、地区...等的功能)
几个函数中的每一个都在其自己的窗口中打开一个不同的编辑器页面,目标是任何给定类型(其中 type="ContractEditor")在任何给定时都应该只打开一个窗口时间。
如果编辑器(在本例中为 Contract Editor)被重复使用,我们希望首先关闭前一个窗口,以便随后的 window.open
将导致新窗口出现在任何窗口的前面(有重点)。
一切都按预期工作,直到页面闲置大约 10 分钟。之后,警报消息显示为:调用的对象已与其客户端断开连接
各种测试表明,异常来自将 window.open 的返回值赋值给 ContractWindow 变量。如果我省略 ContractWindow =
并只调用 window.open
,它可以工作,但窗口不会“弹出”到最前面,这是一项要求。 (window.open 重用名为“ContractEditor”的现有窗口,但不会从打开的窗口堆栈中取出它。)
最佳答案
虽然更好的答案当然是受欢迎的,但我已经找到了一个“变通办法”,可以让这个功能“走出门外”,尽管我想更好地理解这个异常的原因和一个“更好的解决方案” ' 如果它存在。
在确定问题是由于尝试将 window.open
的返回值分配给 var ContractWindow
后,我引入了一层错误方向,如下所示:
var ContractWindow;
function showContract(ContractID){
try{
if(ContractWindow){
if(!ContractWindow.closed){
ContractWindow.close;
}
}
// assign the return of window.open to a 'guaranteed new' variable...
var newWindow = window.open(contract.aspx?contractid=" + ContractID, "ContractEditor",
"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=780,height=615");
// then re-assign to the global var ContractWindow.
ContractWindow = newWindow;
}
}catch(exc){
alert(exc.message);
}
}
我认为 JavaScript 管理对象指针的方式一定有所不同...... 我所做的“有效”,但这不是我所说的“好”...... 然后,墨菲定律总是有这个推论:
If it's stupid but it works, it isn't stupid.
关于window.open(...) 上的 JavaScript 异常 "The object invoked has disconnected from its clients.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669868/