设置当前网页为浏览器首页

设置浏览器主页,在IE浏览器中点击效果如下:

设置当前网页为浏览器首页_第1张图片

在火狐浏览器中效果如下:

设置当前网页为浏览器首页_第2张图片

要成功设置首页可能要把一些杀毒软件和软件管家之类(例如360之类)的关闭。

简单的代码实现如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>设置浏览器主页</title>
		<script type="text/javascript">
			var Yl = {
				setHome: function(obj, hostname) {
					if (!Browser.isIE) {
						alert("您的浏览器不支持自动设置主页,请使用浏览器菜单手动设置。或使用'Ctrl+D'进行添加到收藏夹。")
						return;
					}
					var host = hostname;
					if (!host) {
						host = window.location.href;
					}
					obj.style.behavior = 'url(#default#homepage)';
					obj.setHomePage(host);
				}
			}
			Browser = (function() {
				var H = navigator.userAgent,
					F = 0,
					E = 0,
					I = 0,
					D = 0,
					A = 0,
					_ = 0,
					C = 0,
					B;
				if (H.indexOf("Chrome") > -1 && /Chrome\/(\d+(\.d+)?)/.test(H)) C = RegExp.$1;
				if (H.indexOf("Safari") > -1 && /Version\/(\d+(\.\d+)?)/.test(H)) F = RegExp.$1;
				if (window.opera && /Opera(\s|\/)(\d+(\.\d+)?)/.test(H)) I = RegExp.$2;
				if (H.indexOf("Gecko") > -1 && H.indexOf("KHTML") == -1 && /rv\:(\d+(\.\d+)?)/.test(H)) A = RegExp.$1;
				if (/MSIE (\d+(\.\d+)?)/.test(H)) D = RegExp.$1;
				if (/Firefox(\s|\/)(\d+(\.\d+)?)/.test(H)) _ = RegExp.$2;
				if (H.indexOf("KHTML") > -1 && /AppleWebKit\/([^\s]*)/.test(H)) E = RegExp.$1;
				try {
					B = !!external.max_version
				} catch ($) {}

				function G() {
					var _ = false;
					if (navigator.plugins)
						for (var B = 0; B < navigator.plugins.length; B++)
							if (navigator.plugins[B].name.toLowerCase().indexOf("shockwave flash") >= 0) _ = true;
					if (!_) {
						try {
							var $ = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
							if ($) _ = true
						} catch (A) {
							_ = false
						}
					}
					return _
				}
				return ({
					isStrict: document.compatMode == "CSS1Compat",
					isChrome: C,
					isSafari: F,
					isWebkit: E,
					isOpera: I,
					isGecko: A,
					isIE: D,
					isFF: _,
					isMaxthon: B,
					isFlash: G(),
					isCookie: (navigator.cookieEnabled) ? true : false
				})
			})()
		</script>
	</head>
	<body>
		<a onclick="Yl.setHome(this,this.href); return false;" target="_self" href="#">把当前页设为主页</a>
	</body>

</html>


你可能感兴趣的:(js,浏览器,IE,设置首页,当前网页)