HTML DOM Script Object

HTMLWeb DevelopmentFront End Technology

The DOM Script Object represent the <script> element of an HTML document.

Create script object

Syntax

Following is the syntax −

document.createElement(“SCRIPT”);

Properties of script object

Property
Explanation
async
It returns and alter whether the script should be executed asynchronously or not.
charset
It returns and modify the value of the charset attribute of a script element in an HTML document.
defer
It returns and modify the value of the src attribute of a script element in an HTML document.
src
It returns and modify whether the script should be executed when the page has finished parsing.
crossOrigin
It returns and modify the CORS setting of a script in an HTML document.
text
It returns and modify the text of all the text nodes that are children of a script in an HTML document.
type
It returns and modify the value of the type attribute of a script element in an HTML document.

Example

Let us see an example of script object −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   body{
      text-align:center;
      background-color:#fff;
      color:#0197F6;
   }
   h1{
      color:#23CE6B;
   }
   .drop-down{
      width:35%;
      border:2px solid #fff;
      font-weight:bold;
      padding:8px;
   }
   .btn{
      background-color:#fff;
      border:1.5px dashed #0197F6;
      height:2rem;
      border-radius:2px;
      width:60%;
      margin:2rem auto;
      display:block;
      color:#0197F6;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM Script Object Demo</h1>
<button onclick="createScript()" class="btn">Create a script object</button>
<script>
   function createScript() {
      var scriptElement = document.createElement("SCRIPT");
      scriptElement.innerHTML="confirm('Are you able to create a script element?')";
      document.body.appendChild(scriptElement);
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “Create a script object” button to create a script object:

raja
Published on 19-Jul-2019 16:37:29
Advertisements