Reputation:
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?
Upvotes: 0
Views: 2407
Reputation: 2248
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'
);
Upvotes: 3
Reputation: 2449
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');
Upvotes: 1