0

I'm trying to call Javascript function inside controller action method, Is there any right way to call setTimeout() to be invoked on certain condition inside controller action method ?

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

I need to write above js function inside controller action method, how to write this ?

2 Answers 2

1

Index.csHtml

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

now Ajax Call function

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

Call ajax like this

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

return from controller

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

Execute js function when controller return success

Sign up to request clarification or add additional context in comments.

Comments

0

You should register your javascript function like this:

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

source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.