Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript calling a function with params on window object

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);
        }
    }
});
like image 601
Maxrunner Avatar asked May 03 '13 17:05

Maxrunner


2 Answers

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

like image 164
PSL Avatar answered Sep 21 '22 18:09

PSL


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

like image 27
Rick Viscomi Avatar answered Sep 22 '22 18:09

Rick Viscomi