Use JavaScript files in CakePHP.
At that time, I saved it in webroot/js/.
In that JavaScript file, there is a situation where you specify the path of the image file to use the image file.
However, even if you specify .../img/○○.jpg, it doesn't seem to work.
How should I specify it?
For example, if it is a ctp file to be used in View, I think you can specify it using $this->Html->image().
However, I don't know how to specify it in the JavaScript file.
Thank you for your cooperation.
If you write any path in the JavaScript file under webroot/js/,
It is the path seen from the accessed URL of the HTML file from which the file was loaded.
In the Example 1js file, "img/a.png"
http://foo.example.com/user/img/a.png
,
when accessed by http://foo.example.com/user/
http://foo.example.com/user/index/img/a.png
when accessed by http://foo.example.com/user/index/
In the Example 2js file, "/img/a.png"
http://foo.example.com/img/a.png
or http://foo.example.com/user/index/
whichever you access, http://foo.example.com/user/
However, if you have apps under a specific subdirectory with features such as Alias, (like http://foo.example.com/appbase/user/
) will also be http://foo.example.com/img/a.png
.
Example 1 "img/a.png"
in the js file
http://foo.example.com/user/img/a.png
,
when accessed by http://foo.example.com/user/
http://foo.example.com/user/index/img/a.png
when accessed by http://foo.example.com/user/index/
Example 2 In the js file "/img/a.png"
http://foo.example.com/img/a.png
or http://foo.example.com/user/index/
whichever you access, http://foo.example.com/user/
However, if you have a application under a specific subdirectory with features such as Alias, it will also be http://foo.example.com/img/a.png
.
Perhaps the questioner is operating under a subdirectory.
In that case, you need to supplement the underlying directory, such as /appbase/img/a.png
, or pass $this->request->base
to call processing from a script in the View template.
The image file you want to specify is
webroot/img
From js,
/img/○○.jpg
can be specified as
© 2025 OneMinuteCode. All rights reserved.