I've my Laravel javascript file in public/js folder and it is referred like follows.
<script src="{{ asset('public/js/test.js') }}"></script>
But it is still not found 404.
You shouldn't need to include public in the URL. This should work fine;
<script src="{{ asset('/js/test.js') }}"></script>
What is ASSET_URL value? You can check it in the web developer tool in chrome or firefox. You can configure the asset URL host by setting the ASSET_URL variable in your .env file. This can be useful if you host your assets on an external service like Amazon S3 (https://laravel.com/docs/7.x/helpers#method-asset). For eg
// ASSET_URL=http://example.com/assets
asset('public/js/test.js') //http://example.com/assets/js/test.js
Usually your run your server document root as public folder. Hence, I am guessing that removing the public dir path will solve 404 file not found error i.e change to.
<script src="{{ asset('js/test.js') }}"></script>
Please use instead of as public folder is added Automatically by asset
Related
Could someone help me figure out why this import is not working? Even when I do script/import.js it will not work. I am getting this error message: 127.0.0.1 - - [09/Sep/2020 15:09:35] "GET /import.js HTTP/1.1" 404. Given the file structure in attached image... what am I missing :D
You want to access a file located on a folder who is located in the folder one level up from the current folder, so you should do this :
<script src="../script/import.js"></script>
I'm assuming that you are using Flask for your app, in which case you will need to serve your javascript files as 'static' files, see: https://flask.palletsprojects.com/en/1.1.x/tutorial/static/
Move your 'script' folder into a new folder called 'static', then change your script import to:
<script type="text/javascript" src="{{ url_for('static', filename='script/import.js') }}"></script>
I have tried all answers available but none can solve my problem. I have a html page in which I am drawing svg using js. now I want to store this js in external file and call it (my html and js files are in same folder).
<script src="show1.js"></script> on doing so, my internal js cannot find functions defined in external js and gives 'ReferenceError: DrawLine is not defined'. I even tried using alert in external js to check whether it was being loaded or not, but even alert is not working. is there any settings that i will have to check? kindly help.
My code is really huge. I am posting this snippet instead that shows the internal and external js.
<head>
<script>
//only variables are declared
<script>
</head>
<body>
<script src="show1.js"></script>
<script> drawline(10,10,20,20,'black',4); </script>
</body>
show1.js:
function drawline(x1,y1,x2,y2,c,w)
{
//do stuff
}
Note:these pages are part of django project, and so my html and js are stored in the template folder of the project.
you can use the static file app in django to serve external static files: https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/
You can create a folder on you project to host your static assets (with the name static for example), then you need to configure your project to serve static files in the settings
STATIC_ROOT = 'path to your folder/static/'
STATIC_URL = '/static/'
then in your template:
{% load staticfiles %}
<head>
<script>
//only variables are declared
<script>
</head>
<body>
<script src="{% static 'show1.js' %}"></script>
<script> drawline(10,10,20,20,'black',4); </script>
</body>
in development mode, the runserver command will serve your static files. you may want to serve the files directly from the web server in production mode. also settings may vary depending on the django version, you can refer to the documentation of your version for further details.
I run a local installation of xampp on which a laravel 5.2 app is running. I am trying to include css and js files from the public directory, but none of the content of these files is being loaded.
Laravel finds the files, they are just always empty.
I tried to clear the browser cache and the Laravel cache, but neither worked. I have referenced the files in the following ways:
<script type="text/javascript" src="{{ asset('js/all.js') }}"></script>
OR
<script type="text/javascript" src="/js/all.js"></script>
In both cases the files are loaded, but does not contain anything.
I have tried generating the files through elixir or simply putting them directly in the folder, but still the same result.
Any help is appreciated.
Try this
<script type="text/javascript" src="{{ url('js/all.js') }}"></script>
Try this,
<script type="text/javascript" src="{{ URL::asset('js/all.js') }}"></script>
Here your js directory should be in the laravel's app/public folder and URL::asset() will produce the necessary url for you.
You can refer How to include css and js in Laravel 5 for more info.
Okay, so I didn't exactly solve the issue, but I got around it. Just posting answer for future reference.
I set up a virtual host i xampp to get around the files loading from localhost, like in this example: https://www.codementor.io/php/tutorial/how-to-install-laravel-5-xampp-windows
Afterwards the js and css files load on all the different options presented above. Apparently it is something to do with the .htaccess file in the root, when running from the default folder in xampp.
Thanks for all the suggestions.
Have you tried this?
<script src="{{URL::asset('/js/jquery-1.10.2.min.js')}}"> </script>
Make sure that the 'js' folder is inside the 'public' folder of your project.
I try to achieve a simple goal: add developped dojo classes in a symfony2 html page with Twig.
I had read a lot of documentation on assetic, and i found two types of method to include assets:
The first one with the asset() twig function. And the second one with the "javascripts" tag.
The assets function is used to include files which are stored in the web/ folder whereas javascripts tag load files which are stored in the resource folder of the current bundle.
My dojo classes files are stored in the resource folder of a bundle, so i tried to load them with a javascripts tag like this:
{% javascripts '#MyBundle/Resources/public/js/MyClass.js' output= 'js/myBundle/MyClass.js' %}
<script type='text/javascript' src='{{ asset_url }}'></script>
{% endjavascripts %}
And it work, my file is successfully included, unfortunately the name is not "MyClass.js" but "MyCmass_MyClass_1.js". I have executed the assetic:dump --env=prod command but my file name still "MyCmass_MyClass_1.js"
What can I do to correct this ?
(I tried to delete the cache, relaunch my server in prod/dev, launch the assetic command with the dev env, and no change).
Visit your site in production mode: yoursite.com/app.php instead of yoursite.com/app_dev.php, or point your webserver to correct file in web folder: web/app.php.
I have a Symfony2 project and I have some JavaScript code to be executed.
Where should the .js file be stored in the project and how to call this file (instead of putting the entire script in the .twig)?
Put it in web/js, then access it in your html via:
<script src="{{ asset('js/script.js') }}"></script>
or consider using Assetic.