0

I tried this in the layout file

$(document).ready(function(){ alert("hello"); } );

How do I get this simple thing working? I included jquery in the layout file, using echo $this->headScript()->appendFile(....)

Is there a way to use jquery as is? Without using zendx?

3
  • Why don't you use a separate file for the javascript? Commented Sep 6, 2011 at 14:07
  • You don't need to use ZendX, can you provide code to show how you attempted to do it. Commented Sep 6, 2011 at 14:08
  • I included jquery in my layout file, using headScript()->appendFile(..). In the layout file, in head tag, inside the script tag, I put the above code (an alert inside document ready). What am I doing wrong? :( Commented Sep 6, 2011 at 14:11

2 Answers 2

3

What about the following in your layout file:

$this->headScript()->appendFile('/js/jquery.js'); 
$this->headScript()->appendScript('$(document).ready(function() { alert("hello"); });','text/javascript');

What I mostly do is:

$this->headScript()->appendFile('/js/jquery.js');
$this->headScript()->appendFile('/js/application.js');
$this->headScript()->appendScript('application.init();', 'text/javascript');

echo $this->headScript();

Then the application.js:

var application = {
  init : function() {
    //do here anything you like
  }
}

If you want to call a specific function from let's say your controller you add this function to your application JS first:

var application = {
  init : function() {
    //do here anything you like
  },
  bindChangeEventToSomething : function() {
    $('a').click(function() {});
  }
}

Then in your controller you could add in your action:

$this->view->headScript->appendScript(
   'application.bindChangeEventToSomething()',
   'text/javascript'
);
Sign up to request clarification or add additional context in comments.

4 Comments

wow, that worked. Is this the best way to do it? Does this mean I can't add any JS code in the <script> tag, inside <head> if I use Zend?
Also, what if I need to execute some JS function only on a specific view? This is the way to do it too?
ok, thanks. How do I call the init function in my individual controllers? in the init function?
No the init function should be called once in the layout. This function binds everything to specific elements on the page. You could test if an element exists before binding anything to it. You could use application.js for general purposes and make a controllername.js file for each controller and do almost the same as above with application = { ... }. A example of my application.js: pastebin.com/c3QvC5Hb
1

You should be able to include jQuery without using ZendX. If you would like it to be included in every page add it in the bootstrap or in a controller if it's specific. Here's an example of what putting it in the bootstrap would look like :

protected function _initView(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $view->headScript()->appendFile('/js/jquery.min.js');
}

If you wish to include a js file in just a single controller/action you can include it the init/action of the controller.

$this->view->headScript()->appendFile('/js/index.js');

2 Comments

Yes, that part is fine, I did that. But I am not sure it is including it correctly, as my alert in document ready isn't working. The path to the js file is correct.
if it's included properly in either the bootstrap or the controller you should see it in the head of the page, you don't need to echo in the layout.

Your Answer

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