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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14083872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:40:48  来源:igfitidea点击:

Unable to get property of undefined or null reference

javascriptjqueryinternet-explorer

提问by bnorton

This is similar to my last question but the problem is different. I use a separate javascript file for all of my javascript functions. That file is called by my main window, and is also called in a separate instance by my child windows. My code works with every browser except IE 9 and 10. I have not tested earlier versions of IE.

这与我的最后一个问题类似,但问题不同。我为所有 javascript 函数使用单独的 javascript 文件。该文件由我的主窗口调用,也由我的子窗口在单独的实例中调用。我的代码适用于除 IE 9 和 10 之外的所有浏览器。我没有测试过早期版本的 IE。

IE says the offending line is window.opener.savetoparent($targetval);My previous code was opener.savetoparent($targetval);and before that I simply made the changes to the parent from the child directly. I have also gone into IE and enabled protected mode as suggested in another article with no change in behavior. Savetoparent()is available to both the child and the parent so I must call it with opener for it to run in the parent.

IE 说违规行是window.opener.savetoparent($targetval);我以前的代码opener.savetoparent($targetval);,在此之前,我只是直接从子项对父项进行了更改。我也进入了 IE 并按照另一篇文章中的建议启用了保护模式,行为没有改变。 Savetoparent()对孩子和父母都可用,所以我必须用 opener 调用它才能在父母中运行。

The error I am getting is : Unable to get property 'savetoparent' of undefined or null reference.Here is the code:

我得到的错误是:Unable to get property 'savetoparent' of undefined or null reference.这是代码:

function saveandclose($wintype, $propid) {

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

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

The safe to parent function is:

对父函数的安全是:

function savetoparent($targetval) {
    $('#' + $parentloc).val($targetval);
    var $name = $('#' + $parentloc).attr("name");
    var $rawtargetval = jsstrtonum($targetval);
    processrvsave($propertyid, $name, $rawtargetval);   
    calcrvtotals();
}

Any light you can shed on this would be greatly appreciated.

您可以对此提供任何启发,我们将不胜感激。

window is launched like this

窗口是这样启动的

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'); 
} 

采纳答案by mplungjan

There is no opener in showModalDialog. Use the returnValue

showModalDialog 中没有开启器。使用返回值

Also there has not been a modal parameter on window.open in many years..

多年来,window.open 上也没有模态参数。

Here is how to use returnValue

这是如何使用 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'); 
} 

then

然后

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();
}