1

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';
}
1
  • 1
    If you're running getElementById() before the id is rendered, it's not going to find it. Try putting the echo $this->Html->script() script line at the bottom of the layout (not view) just to see if it works that way. Or even at the bottom of the view should work. (though ideally this should be going into some kind of "onload" event) Commented Jun 4, 2013 at 0:14

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.