日期:2014-05-16  浏览次数:20403 次

一些比较实用的javascript方法
动态加载javascript文件
<script type="text/javascript">
//<!--
/*动态加载方法*/
function loadScript(url, callback){
  var script = document.createElement("script");
  script.type = "text/javascript";
  if(script.readyState){
  //IE
    script.onreadystatechange = function (){
      if(script.readyState == "loaded" || script.readyState == 'complete'){
        script.onreadystatechange = null;
        callback();
      }
    };
  }else{
  //其他浏览器
    script.onload = function (){
      callback();
    };
  }
  script.src = url;
  document.getElementsByTagName("head")[0].appendChild(script);
};
/*
 * 以下代码是先加载jquery-1.5.1.min.js,等待jquery-1.5.1.min.js加载完之后
 * 再加载file2.js,以此类推最后加载file3.js
*/
loadScript("js/jquery-1.5.1.min.js", function(){
  loadScript("file2.js", function(){
    loadScript("file3.js", function(){
      alert("All files are loaded!");
    });
  });
});
//-->
</script>


	function GetXmlHttpReuqest(){
		var xmlHttp=null;
		try
		{
			xmlHttp = new XMLHttpRequest();  //对于firefox chrome等浏览器
		}
		catch(e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //对于IE浏览器
			}
			catch(e)
			{
				try
				{
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					xmlHttp = false;
				}
			}
		}
		
	return false;
	}


//js判断浏览器
<script>
window["MzBrowser"]={};
(function(){
	if(MzBrowser.platform) return;
	var ua = window.navigator.userAgent;
		MzBrowser.platform = window.navigator.platform;
		MzBrowser.firefox = ua.indexOf("Firefox")>0;
		MzBrowser.opera = typeof(window.opera)=="object";
		MzBrowser.ie = !MzBrowser.opera && ua.indexOf("MSIE")>0;
		MzBrowser.mozilla = window.navigator.product == "Gecko";
		MzBrowser.netscape= window.navigator.vendor=="Netscape";
		MzBrowser.safari= ua.indexOf("Safari")>-1;
	if(MzBrowser.firefox) var re = /Firefox(\s|\/)(\d+(\.\d+)?)/;
	else if(MzBrowser.ie) var re = /MSIE( )(\d+(\.\d+)?)/;
	else if(MzBrowser.opera) var re = /Opera(\s|\/)(\d+(\.\d+)?)/;
	else if(MzBrowser.netscape) var re = /Netscape(\s|\/)(\d+(\.\d+)?)/;
	else if(MzBrowser.safari) var re = /Version(\/)(\d+(\.\d+)?)/;
	else if(MzBrowser.mozilla) var re = /rv(\:)(\d+(\.\d+)?)/;
	if("undefined"!=typeof(re)&&re.test(ua))
	MzBrowser.version = parseFloat(RegExp.$2);
})();
function abc()
{
	if(MzBrowser.ie)
	{
		alert('ie');
	}
	if(MzBrowser.firefox)
	{
		alert('firefox');
	}
	alert(MzBrowser.version);
}
abc();
</script>
</head>
<body>
<hr>

<script type="text/javascript">
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	document.write("Browser name: "+ browser);
	document.write("<br>");
	document.write("Browser version: "+ version);
</script>
<hr>

<script type="text/javascript">
	document.write("Browser: ");
	document.write(navigator.appName + "");
	document.write("Browserversion: ");
	document.write(navigator.appVersion + "");
	document.write("Code: ");
	document.write(navigator.appCodeName + "");
	document.write("Platform: ");
	document.write(navigator.platform + "");
	document.write("Cookies enabled: ");
	document.write(navigator.cookieEnabled + "");
	document.write("Browser's user agent header: ");
	document.write(navigator.userAgent + "");
</script>
<hr>
<script type="text/javascript">
	var x = navigator
	document.write("CodeName=" + x.appCodeName);
	document.write("<br>");
	document.write("MinorVersion=" + x.appMinorVersion);
	document.write("<br>");
	document.write("Name=" + x.appName);
	document.write("<br>");
	document.write("Version=" + x.appVersion);
	document.write("<br>");
	document.write("CookieEnabled=" + x.cookieEnabled);
	document.write("<br>");
	document.write("CPUClass=" + x.cpuClass);
	document.write("<br>