Tags:

คือผมกำลังลองทำ Chrome Extension อยู่ครับ (ใช้พวก JavaScript)
แต่ติดปัญหาว่าไม่สามารถเล่นเสียงเตือนได้
จากที่ค้นในกูเกิลดูก็เจอส่วนใหญ่ประมาณนี้

<embed src='chimes.wav' autostart='false' hidden='true' loop='true' name="sound" id="sound">
...
...
...
function userAlert()
{
alert("This is an alert");
document.getElementById("sound").play(false);
document.getElementById("sound").stop();
}

แต่พอรันมันก็ได้ error นี้มาครับ

** Uncaught TypeError: Object #<an HTMLEmbedElement> has no method 'play' **

ใครพอจะแนะนำวิธีแก้ หรือมีโค้ดแบบอื่นบ้างมั้ยครับ

*** แก้ไขครับ ทำได้แล้ว ***

<span id="dummy"></span>
...
...
...
function playSound(soundfile) {
document.getElementById("dummy").innerHTML="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}

ปล. ถ้าจะใส่ code ที่เป็น html tag ต้องใส่ยังไงหรอครับมันถึงจะแสดง คือมันจะหายไปเลยอ่ะครับ ในโค้ดตัวอย่าง ตรง span

Get latest news from Blognone
By: 21Aki
ContributorAndroidWindows
on 11 September 2010 - 22:04 #209136
21Aki's picture

โค้ดที่แก้คือการแทนที่ครับ แต่ถ้าจะแทรกโค้ดเข้าไป หรือต่อท้าย ก็ใช้ + เพื่อแทรกโค้ดใน Tag ได้ครับ หรือตามแบบ Code ด้านล่าง

document.getElementById("dummy").innerHTML+="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";