0

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?

MACMAN
  • 1,883
  • 1
  • 21
  • 35
  • 1
    Are you trying to add the JS to the public site or the control panel? As far as I know, the above is for adding it to the control panel. Have you checked if it outputs a ` – M. Eriksson Nov 02 '22 at 09:10
  • I want to do it in a public site. Thanks – MACMAN Nov 02 '22 at 09:12
  • 1
    As the [documentation](https://statamic.dev/extending/control-panel#adding-css-and-js-assets) says, that is for adding it to the control panel (not the public site). I guess how to add it to the public site depends on the theme you're using? – M. Eriksson Nov 02 '22 at 09:15

1 Answers1

1

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.

Duncan McClean
  • 130
  • 2
  • 11