JS播放声音 兼容所有浏览器
JS播放声音 兼容所有浏览器
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JS播放声音 兼容所有浏览器</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="龚清林 - http://www.gongqinglin.com" /> <script type="text/javascript" src="http://www.gongqinglin.com/js/jquery-1.7.2.min.js"></script> </head> <body> <script language="javascript"> playSound(); function playSound() { var borswer = window.navigator.userAgent.toLowerCase(); if ( borswer.indexOf( "ie" ) >= 0 ) { //IE内核浏览器 var strEmbed = '<embed name="embedPlay" src="http://www.gongqinglin.com/accessory/ding.wav" autostart="true" hidden="true" loop="false"></embed>'; if ( $( "body" ).find( "embed" ).length <= 0 ) $( "body" ).append( strEmbed ); var embed = document.embedPlay; //浏览器不支持 audion,则使用 embed 播放 embed.volume = 100; //embed.play();这个不需要 } else { //非IE内核浏览器 var strAudio = "<audio id='audioPlay' src='http://www.gongqinglin.com/accessory/ding.wav' hidden='true'>"; if ( $( "body" ).find( "audio" ).length <= 0 ) $( "body" ).append( strAudio ); var audio = document.getElementById( "audioPlay" ); //浏览器支持 audion audio.play(); } } </script> </body> </html>
转摘:http://blog.csdn.net/gongqinglin/article/details/49365729