javascript calling a function with params on window object

二次信任 提交于 2019-12-13 02:16:51

问题


So I'm trying to call this method:

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

With the window object:

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

But this doesn't seem to work. It's only when the method has no parameters. Is there a way to work using the window [ variable ] () syntax?

Edit;

ok here's the code:

moarada.jsp has the code with these methods:

<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){ alert(idPlaceHolder); Ext.onReady(function(){ action = "${pageContext.request.contextPath}/moradaPopupLocaleAware.do2"; action += "?method=edit&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);
    });

};

The method ExtWindowAll eventually call code from another js file, that results calling a close window event, with the string of the method name(refreshMoaraLabel) including possible params:

winchoose.on('close', function( p ) { if(functionOnClose) {
alert("method: "+functionOnClose); var substr=functionOnClose.match(/(([^)]*))/); var param=''; if(substr!=null){ param=substr[1]; param="'"+param+"'"; }

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

回答1:


Try this way:-

Window Context needs to take the function name as string.

 window ["refreshMoradaLabel"]();

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

Instead you are trying to invoke the method inside the window context.

window [ refreshMoradaLabel('id') ] (); when you do this you are trying to invoke the result of refreshMoradaLabel('id') which is undefined. since refreshMoradaLabel('id') gets executed first before even reaching the funciton call () of window..




回答2:


The window object contains a property of name refreshMoradaLabel. To access the property, we can use either dot or square bracket notation:

window.refreshMoradaLabel or window['refreshMoradaLabel']

The value of that property is a function. To invoke it, we add parentheses: window.refreshMoradaLabel('id') or window['refreshMoradaLabel']('id').



来源:https://stackoverflow.com/questions/16364468/javascript-calling-a-function-with-params-on-window-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!