HTML DOM write() ให้ผู้ใช้มีฟังก์ชันในการเขียนหลายนิพจน์ (HTML หรือ JavaScript) ลงในเอกสารโดยตรง
หมายเหตุ - วิธีการนี้จะเขียนทับโค้ด HTML ในเอกสาร หากมี และไม่ผนวกอาร์กิวเมนต์ในบรรทัดใหม่
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
document.write(arguments)
ตัวอย่าง
ให้เราดูตัวอย่างของวิธีการเขียนเอกสาร HTML DOM -
<!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> ผลลัพธ์
คลิก 'ไปที่' ปุ่มพร้อมชื่อและนามสกุลที่ไม่ได้รับอนุญาต -

คลิก 'ไปที่' ปุ่มพร้อมชื่อและนามสกุลที่ได้รับอนุญาต -
