laketuna
laketuna

Reputation: 4090

JavaScript & CakePHP: how to get this simple script working?

I'm trying to use echo $this->Html->script('dynamic_emails'); at the very beginning of the View file (ctp) to load some JavaScript, but it is currently not working. It does work if I explicitly put in place <script type="text/javascript" src="/mysite/js/dynamic_emails.js"></script> at the end of the View file.

Here is the code. I'm suspecting that function is not declared properly.. Any ideas?

dynamic_emails.js:

var mytextbox = document.getElementById('mytext');
var mydropdown = document.getElementById('dropdown');

mydropdown.onchange = function(){
     mytextbox.value = 'some text';
}

Upvotes: 1

Views: 250

Answers (2)

Kevin Gorjan
Kevin Gorjan

Reputation: 1332

Depending on your version on cake your js should be in app/webroot/js/dynamic_email.js try putting it there and then including it with the helper , or just include it directly

Also it is usually best practice to include js at the bottom of your files and even better at the bottom of your layout

  • If you are already using something like cake this is probably a medium to large project in wich case you would really benefit from using something like jquery

Upvotes: 1

toby1kenobi
toby1kenobi

Reputation: 1701

Does the code emitted by the helper result in a valid script tag (pointing correctly to your Javascript file), is the Javascript file downloaded by the browser? It's worth checking that the output is as you're expecting.

To be honest, I'm not sure that using the CakePHP helper here really gains you that much anyway, you could just include the full script tag.

Upvotes: 1

Related Questions