Can't load Js files on default.ctp

Hi there, so i’m trying to include a js file on my default.ctp but it’s not working. It works when i include it on my views, but for some reason it doesn’t work with default.ctp. I’m working with apache on Windows
httpd.conf looks like this:
DocumentRoot “C:/xampp/htdocs/myapp/webroot”
<Directory “C:/xampp/htdocs/OdontoQuiz”>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted

<VirtualHost *:80>
DocumentRoot “C:/xampp/htdocs/myapp/webroot”
ServerName myapp.localhost

Sorry for my english. Any help is aprecciated.

This isn’t likely to have anything to do with your Apache configuration. What does the code look like where you include the JS file from your view vs your layout?

Well its mostly the same. If i use $this->Html->script() on default.ctp doesnt show on the “view source code” from chrome it. Not the case if i do it on one of my views. For example index.ctp. Oh right, doesnt work on an element neither. The thing is, it only happend with js, the css works perfectly.

There are many important details that you have glossed over in your description. The problem is somewhere in what you haven’t shown, so we can only make random guesses, and most won’t bother. Help us help you. Please show the actual code.

You should follow the cakephp book convention to add your js,css,images files in the webroot folder.
cakephp easiest way include js,css, image. hope you understand.

well, Post the some code so that you can get right direction to your solution for Problem

Yes, sorry.
This is default.ctp
This is how view source looks like
So i added to my index.ctp doing the same
And it’s actually included without problems

But is your index-styles.css in webroot/css/index-styles.css?

Looks to me like index-styles.css is in the HTML output in both cases. Have I misunderstood the problem?

My index-styles.css is in webroot/css. But the problem is the js files. In this case, ‘menu’, i used the helper in the exact same way but it doesnt work on default.ctp. I just can’t understand why i can use font,css, images but not the javascript files. The only thing i came up with is some sort of security problem but i didn’t change anything.

Maybe try it like this (in the default.ctp):

<?= $this->Html->script('main'); ?> 

note the lack of [] when defining it.
idk if it really matters but it’s worth trying.

I came to this conclusion by looking at the tag where you include index-styles.css as it has the .css extention specified specifically.
Of course, if you remove the [] from the tag where you include the index-styles.css you can remove the .css extention there as well :slight_smile:

1 Like

In your config/app.php you have entries like

'App' => [
  'cssBaseUrl' => 'css/',
  'jsBaseUrl' => 'js/'

With that something like


would generate a script tag containing the URL like

You can also use an array as a parameter, so

$this->Html->script(['test2.js', 'test3.js']);

would generate 2 script tags with a URL like and

1 Like