通过js判断判断电脑是否连上网络

 最近家中晚上经常断网,通过下面的小程序(其实是我组合了两个小程序写的,谈不上原创)可以实现联网时通过播放音乐通知我,不过这其实是由自动订票通知想到的,由于是上班时间写的,所以这个程序还有很多不足的地方,不过已经能完成最基本的功能了:

<html>
	<head>
		<title>联网自动提示器</title>
		<SCRIPT language=JavaScript>
<!--
var millionsecond = 1000;//默认1秒钟检查一次
var media;
var player;
var exp;
var playPositionStart;
var totalTime;

function initMedia(url)
{ 
exp=getExp(url);
if(exp=="mp3" || exp=="wma")
{
   var i_player_str = "<OBJECT id=\"player\" codeBase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 type=application/x-oleobject height=0 standby=\"Loading Microsoft? Windows Media? Player components... \" width=0 classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6><PARAM NAME=\"URL\" VALUE=\""+url+"\"><PARAM NAME=\"UIMode\" VALUE=\"full\"><PARAM NAME=\"AutoStart\" VALUE=\"false\"><PARAM NAME=\"Enabled\" VALUE=\"true\"><PARAM NAME=\"enableContextMenu\" VALUE=\"false\"></OBJECT>";
}
else if(exp=="flv")
{
   var i_player_str = "<OBJECT id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://active.macromedia.com/flash2/cabs/swflash.cab#version=7,0,0,0\" WIDTH=0 HEIGHT=0><PARAM NAME=movie VALUE=\"swf/flvplayer.swf?url="+url+"\"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED name=\"player\" src=\"swf/flvplayer.swf?url="+url+"\" quality=high bgcolor=#FFFFFF WIDTH=0 HEIGHT=0 TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></EMBED></OBJECT>";

}
media=document.getElementById("media");
media.innerHTML=i_player_str;
playobj=document.getElementById("player");

}
//播放音乐
function doplay(){
	if(exp=="mp3" || exp=="wma"){
		playobj.controls.play();
	}else if(exp=="flv"){
		playobj.gotoFrame(7);
	}
}
//得到文件格式
function getExp(url){
	var tmp=url.split('.');
	return tmp[tmp.length-1];
}

//重写String的trim方法
String.prototype.trim = function() {
	 return this.replace(/\s/g,"");
}
//返回访问url的状态结果
function getUrlState(URL){ 
	if(URL.trim().length==0){
		return false;
	}

	var xmlhttp = new ActiveXObject("microsoft.xmlhttp"); 
	xmlhttp.Open("GET",URL, false);  
	try{  
		xmlhttp.Send(); 
	}catch(e){
	}finally{ 
		var result = xmlhttp.responseText; 
		if(result){ 
			if(xmlhttp.Status==200){ 
				return(true); 
			}else{ 
				return(false); 
			} 
		}else{ 
			return(false); 
		} 
	} 
}

var timer ;
function startUrl(){
	var mp3 = document.getElementById("music").value;
	initMedia(mp3);
	//每隔millionsecond执行一次
	timer =  window.setInterval("checkUrl()",millionsecond);
	
}
//检查url,相当于ping,如果能ping通就注销Interval同时播放音乐
function checkUrl(){
	if(getUrlState(document.getElementById("url").value)){
		window.clearInterval(timer);
		doplay();
	}
}
//暂停播放
function stopMusic(){
	playobj.controls.pause();
}
//-->

</SCRIPT>

	</head>
	<body>
		<DIV id=media></DIV>
		请选择音乐:<input type="file" id="music" /><br/>
		请输入网址:<input type="text" value="http://www.baidu.com/" id="url" /><br/>
		<input type="button" id="start" onclick="startUrl()" value="开始测试">
		<input type="button" id="start" onclick="stopMusic()" value="停止音乐">
	</body>
</html>

 

 

你可能感兴趣的:(js,音乐,自动,检查,上网)