无法获取未定义或空引用的属性

3

这与我的上一个问题类似,但问题不同。我使用单独的javascript文件来处理所有的javascript函数。该文件由我的主窗口调用,并且也由我的子窗口在另一个实例中调用。我的代码可以在除IE 9和10之外的所有浏览器中正常工作。我还没有测试早期版本的IE。

IE说有问题的行是window.opener.savetoparent($targetval);。我的以前的代码是opener.savetoparent($targetval);,在此之前我直接从子页面对父页面进行更改。我也进入了IE并启用了保护模式,如另一篇文章建议的那样,但行为没有改变。savetoparent()对于子页面和父页面都是可用的,所以我必须使用opener来调用它以在父页面中运行。

我得到的错误是:无法获取未定义或空引用的'savetoparent'属性。以下是代码:

function saveandclose($wintype, $propid) {

switch($wintype) {
    case 'ccdetail':
        var $targetval = $('#cc-total').val();
        var $fieldname = 'closingcoststotal';
        break;
}

window.opener.savetoparent($targetval);
closewindow();
}

安全的 parent 函数是:
function savetoparent($targetval) {
    $('#' + $parentloc).val($targetval);
    var $name = $('#' + $parentloc).attr("name");
    var $rawtargetval = jsstrtonum($targetval);
    processrvsave($propertyid, $name, $rawtargetval);   
    calcrvtotals();
}

任何您能提供的信息都将不胜感激。
窗口是这样启动的。
if(window.showModalDialog) { 
  window.showModalDialog($childname + '.php?ploc=' + $parentloc + '&propid=' + $propid, '', 'dialogWidth: ' + $winwidth + 'px; dialogHeight: ' + $winheight + 'px;') 
} 
else { 
  window.open($childname + '.php?ploc=' + $parentloc + '&propid=' + $propid, '', 'width=' + $winwidth + ', height=' + $winheight + ', modal=yes'); 
} 

你是否在父页面中使用脚本打开了子窗口? - mplungjan
是的,我做了。它作为模态窗口打开。 - bnorton
启动代码如下:if(window.showModalDialog) { window.showModalDialog($childname + '.php?ploc=' + $parentloc + '&propid=' + $propid, '', 'dialogWidth: ' + $winwidth + 'px; dialogHeight: ' + $winheight + 'px;') } else { window.open($childname + '.php?ploc=' + $parentloc + '&propid=' + $propid, '', 'width=' + $winwidth + ', height=' + $winheight + ', modal=yes'); } - bnorton
请查看https://dev59.com/I2445IYBdhLWcg3wZJYn,它似乎是您的问题的showModalDialog。 - Dampsquid
抱歉所有的内容都是内联的。我尝试进行编辑,但时间不够了。 - bnorton
1个回答

2

showModalDialog中没有打开器(opener)。请使用returnValue。

window.open在许多年中没有模态参数。

以下是如何使用returnValue:

if(window.showModalDialog) { 
  $targetval = window.showModalDialog($childname + '.php?ploc=' + $parentloc + '&propid=' + $propid, 
    window,
    'dialogWidth: ' + $winwidth + 'px; dialogHeight: ' + $winheight + 'px;'))
  if(targetval) savetoparent($targetval);
} 
else { 
  window.open($childname + '.php?ploc=' + $parentloc + '&propid=' + $propid, '', 'width=' + $winwidth + ', height=' + $winheight + ', modal=yes'); 
} 

那么

function saveandclose($wintype, $propid) {
  var $targetval ="";
  switch($wintype) {
    case 'ccdetail':
      $targetval = $('#cc-total').val();
      // var $fieldname = 'closingcoststotal'; I do not see this used anywhere
      break;
  }

  if (window.opener) window.opener.savetoparent($targetval);
  else returnValue = $targetval;
  closewindow();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接