Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

Document Methods ที่รองรับโดย Legacy DOM

Legacy DOM (DOM Level 0)

โมเดลเก่าๆ ที่รองรับโดย Browser ทุกตัว แต่เข้าถึงได้แค่ส่วนสำคัญ: Forms, Form Elements, Images

Document Methods

# Method & Description
1 clear() - Deprecated ล้างเนื้อหาเอกสาร คืนค่า nothing document.clear()
2 close() - ปิด Document Stream ที่เปิดด้วย open() คืนค่า nothing document.close()
3 open() - ลบเนื้อหาเดิม เปิด Stream เขียนเนื้อหาใหม่ คืนค่า nothing document.open()
4 write(value, ...) - เขียน String ลงเอกสารที่กำลัง Parse หรือ Stream ที่ open() คืนค่า nothing document.write(value, ...)
5 writeln(value, ...) - เหมือน write() แต่เพิ่ม Newline ท้าย คืนค่า nothing document.writeln(value, ...)

รายละเอียดแต่ละ Method

document.open()

// เปิด Document ใหม่สำหรับเขียน
document.open("text/html", "replace"); // Type, Replace History
document.write("

Hello

"); document.close(); // ต้อง Close เสมอ

document.write() / writeln()

// ระหว่าง Parsing (ใน HTML)
document.write("

Hello

"); // Insert ที่ Script อยู่ // หลังโหลดเสร็จ (ต้อง open() ก่อน) document.open(); document.write("

New Content

"); document.close();

คำเตือน: ใช้ document.write() หลังโหลดเสร็จจะทับซ้อนหน้าเว็บทั้งหมด!

document.clear()

Deprecated ไม่ควรใช้ ใช้ document.open() แทน หรือ Manipulate DOM โดยตรง

Legacy DOM Properties (เข้าถึงได้)

  • document.forms - HTMLCollection ของ Forms
  • document.images - HTMLCollection ของ Images
  • document.links - HTMLCollection ของ Links (<a href>)
  • document.anchors - HTMLCollection ของ Anchors (<a name>)
  • document.cookie - Cookie String
  • document.title - Title ของหน้า
  • document.URL, document.location - URL
  • document.referrer - Referrer URL
  • document.domain - Domain (Security)
  • document.lastModified - วันที่แก้ไขล่าสุด

สรุป

Legacy DOM Methods: open, close, write, writeln, clear(Deprecated) ใช้สำหรับ Dynamic Document Writing แต่สมัยใหม่ควรใช้ DOM Manipulation (createElement, appendChild, innerHTML) แทน เพราะปลอดภัยและทันสมัยกว่า