繁体   English   中英

在控制器动作-YII中调用Javascript函数

[英]Calling Javascript function inside controller action -YII

我正在尝试在控制器动作方法内调用Javascript函数,是否有正确的方法来调用setTimeout()以在控制器动作方法内的特定条件下调用?

window.setTimeout(function() {  
    alert("test");
    $.ajax({
        type: "POST",
        url:    "'.$this->createUrl("/operator/createViopNode/").'",
        data: {
            id: '.$bc_id.',
            callid:"'.$num.'",
            taskid:'.$this->taskid.'
        },
        success: function(msg){
            var ifrm = document.getElementById("frame");
            ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
            ifrm.document.open();
            ifrm.document.write(msg);
            ifrm.document.close();
        },
        error: function (jqXHR, textStatus, errorThrown){
            alert("" + textStatus + ", " + errorThrown);
        }
    });     
}, parseInt('.$tps_call.'));

我需要在控制器动作方法中编写上述js函数,该如何编写?

Index.csHtml

function abc()
 {
   alert("called")
 }

现在Ajax通话功能

function ExecuteAjax(URL,Data,Success)
{
    try {
        $.ajax({
            type: "post",
            url: URL,
            data: Data,
            contentType: "json",
            success: function (data) { if (typeof Success == "function") { Success(data); } }
        })
    } catch (e) {
        alert(e.message)
    }
}

这样叫ajax

 ExecuteAjax("/Home/FillColorDropDown", "", function (data) {
            eval(data.script);
        });

从控制器返回

      if(demo=="true")//put condition here whatever you want
       {
         string strscript="abc();";
       }
      protected JObject jobj = new JObject();
      jobj.Add("Script", strscript);
      return Json(jobj);

控制器返回成功时执行js函数

您应该像这样注册javascript函数:

function actionTest(){
  $cs = Yii::app()->clientScript;
  $cs->registerScript('my_script', 'alert("Hi there!");', CClientScript::POS_READY);
  $this->render('any_view');
}

资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM