Script

Struct Script 

Source
pub struct Script;
Expand description

The <script> element - embeds or references executable code.

§Purpose

The <script> element embeds executable code or data, or references an external script file. Primarily used for JavaScript, it enables dynamic behavior, interactivity, and client-side functionality in web pages. Scripts can be inline or loaded from external files.

§Content Categories

  • Metadata Content
  • Flow Content
  • Phrasing Content
  • Script-supporting Element

§Permitted Content Model

  • If no src attribute: inline script content matching the type
  • If src attribute: no content or only comments and whitespace

§Common Use Cases

  • Adding interactivity to web pages
  • Manipulating the DOM dynamically
  • Handling user events and input
  • Making AJAX requests and fetching data
  • Implementing client-side application logic

§Key Attributes

  • src: URL of external script file
  • type: MIME type of the script (default: “text/javascript”)
  • async: Execute asynchronously (for external scripts)
  • defer: Defer execution until document parsing is complete
  • crossorigin: CORS settings for script fetching
  • integrity: Subresource integrity hash for security
  • referrerpolicy: Referrer policy for script requests
  • nomodule: Execute only in browsers that don’t support ES modules
  • nonce: Cryptographic nonce for Content Security Policy

§Example

<!-- External script -->
<script src="/js/app.js"></script>

<!-- External script with async loading -->
<script src="/js/analytics.js" async></script>

<!-- External script with deferred execution -->
<script src="/js/init.js" defer></script>

<!-- Inline script -->
<script>
  console.log('Hello, World!');
  document.addEventListener('DOMContentLoaded', function() {
    // Initialize app
  });
</script>

<!-- ES6 module -->
<script type="module">
  import { init } from './modules/app.js';
  init();
</script>

<!-- Script with integrity check -->
<script src="https://cdn.example.com/library.js"
        integrity="sha384-ABC123..."
        crossorigin="anonymous"></script>

<!-- JSON-LD structured data -->
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Organization",
  "name": "Example Company"
}
</script>

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Script

Source§

const TAG: &'static str = "script"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl CanContain<Script> for Dl

Source§

impl CanContain<Script> for Head

Source§

impl CanContain<Script> for Hgroup

Source§

impl CanContain<Script> for Menu

Source§

impl CanContain<Script> for Ol

Source§

impl CanContain<Script> for Optgroup

Source§

impl CanContain<Script> for Picture

Source§

impl CanContain<Script> for Select

Source§

impl CanContain<Script> for Table

Source§

impl CanContain<Script> for Tbody

Source§

impl CanContain<Script> for Tfoot

Source§

impl CanContain<Script> for Thead

Source§

impl CanContain<Script> for Tr

Source§

impl CanContain<Script> for Ul

Source§

impl CanContain<Text> for Script

Source§

impl FlowContent for Script

Source§

impl MetadataContent for Script

Source§

impl PhrasingContent for Script

Source§

impl ScriptSupporting for Script

Auto Trait Implementations§

§

impl Freeze for Script

§

impl RefUnwindSafe for Script

§

impl Send for Script

§

impl Sync for Script

§

impl Unpin for Script

§

impl UnwindSafe for Script

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.