Phương thức canPlayType() của đối tượng Video trong HTML DOM trả về một chuỗi cho biết trình duyệt có thể phát loại video được chỉ định hay không.
Cú pháp
Trả về giá trị boolean – true/false:
videoObject.canPlayType(typeAsParameter)
Ở đây, giá trị trả về có thể là:
| returnValue | Chi tiết |
|---|---|
| probably | Định nghĩa trình duyệt rất có thể hỗ trợ loại video được chỉ định |
| maybe | Định nghĩa trình duyệt có thể hỗ trợ loại video được chỉ định, nhưng cần thử phát để chắc chắn |
| "" (chuỗi rỗng) | Định nghĩa trình duyệt chắc chắn không hỗ trợ loại video được chỉ định |
Ví dụ
Ví dụ minh họa phương thức Video canPlayType():
<!DOCTYPE html> <html> <head> <title>HTML DOM Video canPlayType()</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-Video-canPlayType( )</legend> <video id="demo" width="320" controls><source src="" type="video/mp4"></video><br> <input type="button" onclick="getTrackDetails()" value="Trình duyệt có hỗ trợ video/mp4?"> <div id="divDisplay"> </div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var demo = document.getElementById("demo"); var srcOfMedia = 'https://www.tutorialspoint.com/html5/foo.mp4'; function getTrackDetails() { var ans = demo.canPlayType('video/mp4'); if(ans !== ''){ divDisplay.textContent = 'Trình duyệt hỗ trợ mp4'; demo.src = srcOfMedia; demo.load(); } else divDisplay.textContent = 'Trình duyệt không hỗ trợ mp4'; } </script> </body> </html>
Kết quả
Trước khi nhấn nút „Trình duyệt có hỗ trợ video/mp4?“:

Sau khi nhấn nút „Trình duyệt có hỗ trợ video/mp4?“:
