- concrete5 Cookbook
- David Strack
- 191字
- 2021-08-13 16:16:01
Including JavaScript in block forms
When adding or editing blocks, it is often desired to include more advanced functionality in the form of client-side JavaScript. concrete5 makes it extremely easy to automatically add a JavaScript file to a block's editor form.
Getting ready
We will be working with the block that was created in the first recipe of this chapter. If you need to catch up, feel free to download the code from this book's website.
How to do it...
The steps for including JavaScript in block forms are as follows:
- Open your block's directory.
- Create a new file called
auto.js
. - Add a basic alert function to
auto.js
:alert('Hello!');
- Visit a page that contains your block.
- Enter edit mode and edit the block.
- You should see your alert message appear as shown in the following screenshot:
How it works...
concrete5 automatically looks for the auto.js
file when it enters add or edit mode on a block. Developers can use this to their advantage to contain special client-side functionality for the block's edit mode.
See also
- The Including JavaScript in the block view recipe
- The Including CSS in the block view recipe