javascript在window对象上调用带有参数的函数 [英] javascript calling a function with params on window object

查看:32
本文介绍了javascript在window对象上调用带有参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

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

与窗口对象:

<块引用>

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

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

编辑;

好的,这是代码:

moarada.jsp 有这些方法的代码:

<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) != '') {//更新标签new Ajax.Updater(idPlaceHolder+'_label', 'moradaPopupLocaleAware.do2?method=getLabel',{方法:'get',参数:'id='+$F(idPlaceHolder)});}};

<块引用>

window.editMoradaPopup= function(idPlaceHolder,method){警报(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);});};

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

<块引用>

winchoose.on('close', function( p ) {if(functionOnClose) {
alert("方法:"+functionOnClose);var substr=functionOnClose.match(/((([^)]*)))/);变量参数='';if(substr!=null){参数=substr[1];参数="'"+参数+"'";}

 调试器;如果(窗口[functionOnClose]){窗口[functionOnClose](参数);}}});

解决方案

试试这个方法:-

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

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

相反,您正在尝试调用窗口上下文中的方法.

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

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

解决方案

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

这篇关于javascript在window对象上调用带有参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆