document.links.length ให้จำนวนลิงก์ ( tags) ในเอกสาร ตัวอย่างที่ 1: 3 ลิงก์ <html> <body> <a href=https://www.tutorialspoint.com/javascript/index.htm>Javascript</a><br> <a href=https://www.tutorialspoint.com/php/index.htm>Php</a><br> <a href=htt
document.links.length ให้จำนวนลิงก์ ( tags) ในเอกสาร ตัวอย่างที่ 1: 3 ลิงก์ <html> <body> <a href=https://www.tutorialspoint.com/javascript/index.htm>Javascript</a><br> <a href=https://www.tutorialspoint.com/php/index.htm>Php</a><br> <a href=htt
document.links เป็น collection ของลิงก์ () ในเอกสาร เข้าถึง href ผ่านดัชนีเหมือนอาร์เรย์ ตัวอย่างที่ 1: ลิงก์ที่ 3 (ดัชนี 2) <html> <body> <a href=https://www.tutorialspoint.com/javascript/index.htm>Javascript</a><br> <a href=https://www.tutorialspoint.com/java8/i
document.domain ให้โดเมนของเซิร์ฟเวอร์ที่โหลดเอกสาร ตัวอย่าง <html> <body> <p id=domain></p> <script> document.getElementById(domain).innerHTML = The domain name is: + document.domain; </script> </body> </html> ผลลัพธ์ The domain name is www
document.cookie ให้สตริงของ cookies ทั้งหมดที่เกี่ยวข้องกับเอกสารปัจจุบัน ไวยากรณ์ cookie = document.cookie; ตัวอย่าง <html> <body> <p id=cookie></p> <script> document.getElementById(cookie).innerHTML = Cookies associated with this document: + document.cookie;
screen.availWidth และ screen.availHeight ให้ขนาดพื้นที่ทำงานจริงของหน้าจอ (ลบ taskbar ออก) ในพิกเซล Available Width <html> <body> <p id=width></p> <script> document.getElementById(width).innerHTML = Available screen width is + screen.availWidth; </script>
Flag /g (global) ใช้กับ regex replace เพื่อแทนที่ ทุกครั้งที่พบ ไม่ใช่แค่ครั้งแรก ตัวอย่าง ไม่ใช้ /g (แทนที่แค่ครั้งแรก) <html> <body> <script> var str = Mr Blue has a blue house and a blue car; var res = str.replace(/blue/, red); document.write(res); </script&g
JavaScript เป็น case-sensitive Flag /i (ignore case) ใช้กับ regex replace ทำให้แทนที่คำได้ไม่สนใจตัวพิมพ์เล็ก/ใหญ่ ตัวอย่าง ไม่ใช้ /i (Case-sensitive) <html> <body> <script> var str = Mr Blue has a blue house and a blue car; var res = str.replace(/blue/g, red); docu
ขั้นตอนเพิ่มอีเลเมนต์ใหม่: 1) สร้างอีเลเมนต์ด้วย document.createElement() 2) สร้างข้อความด้วย document.createTextNode() 3) ผูกข้อความเข้ากับอีเลเมนต์ด้วย appendChild() 4) ผูกอีเลเมนต์เข้ากับ parent ด้วย appendChild() ตัวอย่าง <html> <body> <div id=new> <p id=p1>Tutorix
ใช้ removeChild() ลบอีเลเมนต์ลูกออกจากอีเลเมนต์พาเรนต์ ไวยากรณ์ parent.removeChild(childElement); ตัวอย่าง <html> <body> <div id=new> <p id=p1>Tutorix</p> <p id=p2>Tutorialspoint</p> </div> <script> var parent = document.getEl
startsWith() (ES6) ตรวจสอบว่าสตริงขึ้นต้นด้วยข้อความที่กำหนด อ่านง่ายกว่า indexOf() === 0 รองรับพารามิเตอร์ที่ 2 สำหรับตำแหน่งเริ่มต้น ตัวอย่าง indexOf() แบบเก่า <html> <body> <script> var text = Tutorialspoint; document.write(text.indexOf(T) === 0); </script> <
test() เป็น regex method ค้นหารูปแบบในสตริง คืน true ถ้าเจอ false ถ้าไม่เจอ เป็น case-sensitive ตัวอย่างที่ 1: พบรูปแบบ Tu <html> <body> <p id=text>Tutorix is the best e-learning platform</p> <p id=test></p> <script> var text = document.getElementBy
exec() exec() เป็น regex method ค้นหารูปแบบในสตริง คืนค่าออบเจ็กต์ของข้อความที่เจอ (มี index, input) หาไม่เจอคืน null แตกต่างจาก test() ที่คืน true/false ตัวอย่างที่ 1: ค้นหา est <html> <body> <script> var obj = /est/.exec(Tutorix is the best e-learning platform); docume
Getters และ Setters ใช้กำหนด Object Accessors Getter ใช้ดึงค่าพร็อพเพอร์ตี้ Setter ใช้ตั้งค่าพร็อพเพอร์ตี้ Getters <html> <body> <script> var business = { Name: Musk, Country: America, Company: PayPal, get comp() { return this.company; }
การ clone ด้วย assignment operator (=) ทำให้ทั้งสองตัวแปรอ้างอิงออบเจ็กต์เดียวกัน (shallow reference) การเปลี่ยนแปลงตัวหนึ่งจะส่งผลต่ออีกตัว ES6 spread operator (...) สร้าง shallow copy แยกอ้างอิง วิธีเดิม Assignment (Reference) <html> <body> <script> var org = {org1: Servene
มี 2 วิธี: 1) string.repeat() 2) Array.fill().join() 1. string.repeat() string.repeat(number); ตัวอย่าง <html> <body> <script> const str = Tutorix, ; var res = str.repeat(3); document.write(res); </script> </body> </html> ผลลัพธ์ Tutorix, Tutorix,
มี 3 วิธี: 1) string.split() 2) Spread operator (...) ES6 3) Array.from() ES6 1. string.split() string.split(); ตัวอย่าง <html> <body> <script> const str = Tutorialspoint; var d = split(); document.write(d); </script> </body> </html> ผลลัพธ์ T,u,t
มี 2 วิธีหลัก: indexOf() และ includes() (ES6) ทั้งคู่ case-sensitive indexOf() indexOf(str); คืนดัชนี หากไม่พบคืน -1 ตรวจสอบด้วย !== -1 ตัวอย่าง <html> <body> <script> var company = Tutorix; document.write(company.indexOf(Tutor) !== -1); document.write(<br>);
navigator object มี navigator.appName และ navigator.appVersion Application Name ส่วนใหญ่คือ Netscape (IE11, Chrome, Firefox, Safari) <html> <body> <script> document.write(navigator.appName); </script> </body> </html> ผลลัพธ์ Netscape Browser Version <h
navigator object ให้ข้อมูลเบราว์เซอร์ มี navigator.product และ navigator.appCodeName Product Name ส่วนใหญ่คือ Gecko <html> <body> <p id=proname></p> <script> document.getElementById(proname).innerHTML = the product name is + navigator.product; </script>
location object ให้ข้อมูล URL ปัจจุบัน มี window.location.protocol และ window.location.pathname Pathname <html> <body> <p id=pathname></p> <script> document.getElementById(pathname).innerHTML = Page path is: + window.location.pathname; </script> </bod