javascript calling a function with params on window object

javascript, window

Solution

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..

Problem

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

Original source