Google个性化页面中实例化XMLHTTP对象的方法

function x()
{
	var a=null;
	try
	{
		a=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(c)
	{
		try
		{
			a=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(b)
		{
			a=null;
		}
	}
	if(!a&&typeof XMLHttpRequest!="undefined")
	{
		a=new XMLHttpRequest();
	}
	return a;
}

  这个实例化方法至少支持IE、Firefox、Opera和Safari,它的一大特色是用了try…catch…,而不是经常用的if…else…,这种方式是比较稳妥和有效的。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

您正在使用您的 WordPress.com 账号评论。 注销 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 注销 /  更改 )

Connecting to %s