Prerequisites
The.com Basics JavaScript
Intro
Script Elements allow you to add custom JavaScript code to pages. You can also link JS files from external sources with this element.
When in the editor, you can mimic the page loading to reload and rerun and script tags by clicking the refresh button. On the page editor, the refresh button is in the drop-up menu next to the deploy button.
If you don't know JavaScript, start by getting familiar with it.
The Script Element
When first adding a Script Element, you'll notice five text inputs for text, source, type, defer, and async. These correlate to the HTML script tag's attributes.
Text
Write raw JS in this text input. The Script Element supports writing in jQuery as well as long as the jQuery source code is being loaded into a webpage.
You can make this cell full-screen mode by pressing CMD + Enter after double clicking on the text input. This makes it easier to write and edit the script, or copy and paste a script and maintain its formatting.
Source
You can load and run an external script by pasting the URL pointing to a script in this attribute.
Type
This is where you can specify the type of script. Usually, you can leave this blank, but you can also set the type to "module" and use JavaScript modules.
Defer
This Boolean attribute is set to indicate if script is meant to be executed after the document has been parsed, but before firing the DOMContentLoaded
event.
Scripts with the defer
attribute set to "true" will prevent the DOMContentLoaded
event from firing until the script has loaded and finished evaluating.
Async
This Boolean attribute specifies if the script can run in parallel with other scripts or not.
For more details about the HTML script element attributes, check out Mozilla's docs.
Comments
0 comments
Please sign in to leave a comment.