1

How to call javascript function in controller as below

In example.js

function hello(){
  alert("hi");
}

In exampleController.php

function index(){
   hello();
}
2
  • 2
    You can't call JavaScript function from the controller, controller is running on server-side, and your java script code running under the browser/client side, If you want to call javaScript function you must have to render view and call from that. you also try $this->Html->scriptBlock('alert("hi")'); code to call javaScript function from the view. Commented Jun 21, 2017 at 6:46
  • Yeah, you are right. Thanks Commented Jun 21, 2017 at 6:48

1 Answer 1

2

You can't call JavaScript function from the controller,

controller is running on server-side, and your java script code running under the browser/client side.

If you want to call javaScript function you must have to render view and call from that.

You can also try $this->Html->scriptBlock('alert("hi")'); code to call javaScript function from the view.

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

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.