HTML DOM open() Method


The DOM open() method set a node specified in its parameter to an attribute node using its name in an HTML document.

Syntax

Following is the syntax −

document.open(type,replace);

Here type represent type of document and replace represent whether the history entry for the new document inherits the history entry from the document which opened this document or not.

Example

Let us see an example of open() method −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   body{
      text-align:center;
      color:#fff;
      background: #ff7f5094;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM open() Method Demo</h1>
<button onclick="openMethod()" class="btn">Click me to try Open() Mehtod</button>
<script>
   function openMethod() {
      document.open();
      document.write(`
         <style>
            body{
               text-align:center;
               color:#fff;
               background: #ff7f5094;
            }
         </style>
         <h1>DOM open() Method Demo</h1>
         <p>New document</p>
      `);
      document.close();
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on the “blue” button to open a new output stream using open() method.

Updated on: 30-Jul-2019

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements