javascript调用窗口对象上的params函数

时间:2013-05-03 17:23:26

标签: javascript window

所以我试着调用这个方法:

  

refreshMoradaLabel = function(idPlaceHolder){...};

使用窗口对象:

  

window [refreshMoradaLabel('id')]();

但这似乎不起作用。只有当方法没有参数时才会这样。 有没有办法使用window [ variable ] ()语法?

编辑;

好的,这是代码:

moarada.jsp包含以下方法的代码:

<c:set var="methodOnClose" value="refreshDynamicValues" />
<c:if test="${empty fieldInstance || (not empty fieldInstance && isTramitacao)}">
  <c:set var="methodOnClose" value="refreshMoradaLabel(${dfieldId})" />
</c:if>
<a class="texto" href="#" onclick="editMoradaPopup('${dfieldId}','${methodOnClose}');" id="moradas_${dfieldId}"><img alt="${moradaDes}" src="${pageContext.request.contextPath}/images/icon/icon-pesquisa.png"></a>
  

window.refreshMoradaLabel = function(idPlaceHolder){

    alert("label:" +idPlaceHolder);
    if($F(idPlaceHolder) != '') {
        //Update label
        new Ajax.Updater(idPlaceHolder+'_label', 'moradaPopupLocaleAware.do2?method=getLabel', 
                {method: 'get', parameters: 'id='+$F(idPlaceHolder)});
    }

};
  

window.editMoradaPopup = function(idPlaceHolder,method){       警报(idPlaceHolder);   Ext.onReady(函数(){   action =“$ {pageContext.request.contextPath} /moradaPopupLocaleAware.do2”;   action + =“?method = edit&amp; id =”+ $(idPlaceHolder).value;

        action += "&idPlaceHolder="+idPlaceHolder;
        action += "&savemorada=true";
        action += "&window=winchoose";      
        return ExtWindowAll('${moradaDes}',action,'','html',true,true,true,650,400,true,true,'fit', method);
    });

};

方法ExtWindowAll最终从另一个js文件中调用代码,这会调用一个关闭窗口事件,方法名称(refreshMoaraLabel)的字符串包括可能的参数:

  

winchoose.on('close',function(p){           if(functionOnClose){
              alert(“方法:”+ functionOnClose);               var substr = functionOnClose.match(/(([^]] *))/);               var param ='';               如果(SUBSTR!= NULL){                 PARAM = SUBSTR [1];                 PARAM = “ ' ”+ PARAM +“'”;               }

        debugger;
        if(window[functionOnClose]) {
            window[functionOnClose](param);
        }
    }
});

2 个答案:

答案 0 :(得分:2)

尝试这种方式: -

Window Context需要将函数名称作为字符串。

 window ["refreshMoradaLabel"]();

  window ["refreshMoradaLabel"]('id');

相反,您试图在窗口上下文中调用该方法。

window [refreshMoradaLabel('id')]();当你这样做时,你试图调用未定义的refreshMoradaLabel('id')的结果。因为refreshMoradaLabel('id')在到达窗口的函数()之前首先被执行..

答案 1 :(得分:2)

window对象包含名称为refreshMoradaLabel的属性。要访问该属性,我们可以使用点或方括号表示法:

window.refreshMoradaLabelwindow['refreshMoradaLabel']

该属性的值是一个函数。要调用它,我们添加括号:window.refreshMoradaLabel('id')window['refreshMoradaLabel']('id')