Class Script


@Marshalled(typeName="script") public class Script extends HtmlElementRawText<Script>
DTO for an HTML <script> element.

The script element is used to embed or reference executable code, typically JavaScript. It can contain inline script code or reference external script files. The script element is commonly used to add interactivity and dynamic behavior to web pages.

Examples:

import static org.apache.juneau.bean.html5.HtmlBuilder.*; // Inline JavaScript Script script1 = script() .text("console.log('Hello, World!');"); // External JavaScript file Script script2 = script() .src("https://example.com/script.js") .type("text/javascript"); // Async script loading Script script3 = script() .src("https://example.com/analytics.js") .async(true) .defer(true); // Script with integrity check Script script4 = script() .src("https://example.com/library.js") .integrity("sha384-...") .crossorigin("anonymous"); // Module script Script script5 = script() .src("https://example.com/module.js") .type("module");

The following convenience methods are provided for constructing instances of this bean:

See Also:
  • Constructor Details

  • Method Details

    • async

      public Script async(Object value)
      async attribute.

      Execute script asynchronously.

      This attribute uses deminimized values:

      • false - Attribute is not added
      • true - Attribute is added as "async"
      • Other values - Passed through as-is
      Parameters:
      value - The new value for this attribute. Typically a Boolean or String. Can be null to unset the attribute.
      Returns:
      This object.
    • charset

      public Script charset(String value)
      charset attribute.

      Specifies the character encoding of the external script resource. This is used when the script is loaded from an external source.

      Common values:

      • "utf-8" - UTF-8 encoding (default)
      • "iso-8859-1" - Latin-1 encoding
      • "windows-1252" - Windows-1252 encoding
      Parameters:
      value - The character encoding of the external script resource. Can be null to unset the attribute.
      Returns:
      This object.
    • crossorigin

      public Script crossorigin(String value)
      crossorigin attribute.

      How the element handles cross-origin requests.

      Parameters:
      value - The new value for this attribute. Can be null to unset the attribute.
      Returns:
      This object.
    • defer

      public Script defer(Object value)
      defer attribute.

      Defer script execution.

      This attribute uses deminimized values:

      • false - Attribute is not added
      • true - Attribute is added as "defer"
      • Other values - Passed through as-is
      Parameters:
      value - The new value for this attribute. Typically a Boolean or String. Can be null to unset the attribute.
      Returns:
      This object.
    • src

      public Script src(Object value)
      src attribute.

      Address of the resource.

      The value can be of any of the following types: URI, URL, String. Strings must be valid URIs.

      URIs defined by UriResolver can be used for values.

      Parameters:
      value - The new value for this attribute. Typically a URL or String. Can be null, in which case the attribute is stored with a null value (not removed).
      Returns:
      This object.
    • type

      public Script type(String value)
      type attribute.

      Type of embedded resource.

      Parameters:
      value - The new value for this attribute. Can be null to unset the attribute.
      Returns:
      This object.