เมธอด write() ของ HTML DOM ให้ผู้ใช้ฟังก์ชันเขียนนิพจน์หลายอย่าง (HTML หรือ JavaScript) โดยตรงลงในเอกสาร
หมายเหตุ: เมธอดนี้จะทับทับโค้ด HTML ในเอกสารถ้ามี และ ไม่ เพิ่มอาร์กิวเมนต์ลงบรรทัดใหม่
ไวยากรณ์ (Syntax)
document.write(arguments)
ตัวอย่างการใช้งาน
ตัวอย่างต่อไปนี้แสดงการใช้งานเมธอด HTML DOM document write():
<!DOCTYPE html> <html> <head> <title>HTML DOM write()</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } input[type="button"] { border-radius: 10px; </style> </head> <body> <form> <fieldset> <legend>HTML-DOM-write( )</legend> <input id="urlSelect" type="url" placeholder="Type URL here..."><br> <input id="textSelect" type="text" placeholder="Full Name"><br> <input type="button" value="Go To" onclick="openWindow()"> <input type="button" value="Close" onclick="closeWindow()"> <input type="button" value="Restore" onclick="restoreWindow()"> <div id="divDisplay"></div> </fieldset> </form> <script> var urlSelect = document.getElementById("urlSelect"); var textSelect = document.getElementById("textSelect"); var winSource; function openWindow() { browseWindow = window.open(urlSelect.value, "browseWindow", "width=400, height=200"); winSource = urlSelect.value; browseWindow.opener.document.getElementById("divDisplay").textContent = "Child Window Active"; if(textSelect.value !== 'admin') browseWindow.document.write("<p><strong>Unauthorized User: </strong></p>","<p>"+textSelect.value+"</p>"); } function closeWindow(){ if(browseWindow){ browseWindow.close(); browseWindow.opener.document.getElementById("divDisplay").textContent = "Child Window Closed"; } } function restoreWindow(){ if(browseWindow.closed){ browseWindow = window.open(winSource, "browseWindow", "width=400, height=200"); browseWindow.opener.document.getElementById("divDisplay").textContent = "Child Window Restored"; } } </script> </body> </html>
ผลลัพธ์
คลิกปุ่ม 'Go To' โดยชื่อเต็มไม่ได้รับอนุญาต:

คลิกปุ่ม 'Go To' โดยชื่อเต็มได้รับอนุญาต:
