How to include a new JavaScript file to my Statamic application?

661 views Asked by At

I am trying to include a new js file into my Statamic application. As per the Statamic documentation I changed the file AppServiceProvider.php

in the boot method I added

Statamic::script('App', 'newfile.js');

But it doesn't load the javascript file. Is there anything else I have to do to make the script working?

1

There are 1 answers

0
Duncan McClean On

The Statamic::script code allows you to add JavaScript to Statamic's Control Panel, not onto your site, as explained in the Statamic documentation.

To add a JavaScript file to the front-end of your site, you will want to find the layout file in resources/views (possibly called layout.antlers.html).

Then you will want to add the JS file inside the <head> (or bottom of the <body> depending on what you need) of the layout, like so:

<head>
    <script src="/js/new-file.js"></script>
</head>

Make sure you change /js/new-file.js to the path of your JS file.