最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript calling a function with params on window object - Stack Overflow

programmeradmin0浏览0评论

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

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);
        }
    }
});
Share Improve this question edited May 6, 2013 at 16:29 Maxrunner asked May 3, 2013 at 17:23 MaxrunnerMaxrunner 1,9654 gold badges24 silver badges41 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 7

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

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

发布评论

评论列表(0)

  1. 暂无评论