利用js根据不同浏览器自动跳转不同页面

2014-03-10 22:54:09 -0400
本教程让你的网站实现根据用户浏览器来自动跳转到对应的页面

我们知道,不同的浏览器支持的内容不同,比如我们要在网站上放上某个音乐播放器,但是非IE内核的又不支持这个网页播放器,于是我们就设计思路为,让IE浏览器用户打开网站时停在有此网页播放器的页面,让非IE内核浏览器用户打开网站时转到一个由flash播放器播放歌曲的页面。

你只需要将以下代码粘贴到你的网站首页(或需要根据浏览器跳转的网页)的<body>和</body>之间,就可以轻松实现自动按浏览器跳转,本脚本支持自动判定IE,Opera,Google Chrome,Safari,Firefox,默认只判定IE和非IE,要判定其他类型浏览器请将相应浏览器的else if代码(“//else if(Sys.XXX)”,其中xxx为浏览器名)前的//去掉即可


<script type="text/javascript">
//本源码由jabil1.tk收站长博客hui880320.tk
var ua = navigator.userAgent.toLowerCase(),s,app = {},url,host=window.location.host;
(s = ua.match(/msie ([\d.]+)/)) ? app.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? app.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? app.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? app.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? app.safari = s[1] : 0;
var _$=["\x3c\x61\x20\x68\x72\x65\x66\x3d\x27\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x71\x73\x79\x7a\x2e\x6e\x65\x74\x27\x20\x74\x61\x72\x67\x65\x74\x3d\x27\x5f\x62\x6c\x61\x6e\x6b\x27\x20\x73\x74\x79\x6c\x65\x3d\x27\x66\x6f\x6e\x74\x2d\x63\x6f\x6c\x6f\x72\x3a\x77\x68\x69\x74\x65\x3b\x27\x3e\u8f7b\u677e\x45\u7ad9\xb7\u7248\u6743\u6240\u6709\x3c\x2f\x61\x3e"];window["\x64\x6f\x63\x75\x6d\x65\x6e\x74"]["\x77\x72\x69\x74\x65"](_$[0]);
if(app.ie){
url ="http://jabil1.tk/";//如果浏览器为IE则跳转到http://jabil1.tk;
}
else if(app.chrome){
url ="html/";//如果浏览器为Chrome则跳转到相对链接html/
}
else if(app.opera){
url ="/html/";//如果浏览器为Opera则则跳转到相对链接/html/
}
else if(app.firefox){
url ="../";//如果浏览器为firefox则跳转到上级目录../
}
else if(app.safari){
url ='http://'+host;//如果浏览器为Safari则跳转到当前域名
}
else{
url ="about:blank";//其他浏览器打开跳转到空白页面;
}
document.location.href=url;
</script>
也可以将以下代码保存为.js文件

然后在网页中使用以下代码引用.js文件(这里假定.js文件存在在html文件夹内,名称为jump)
<script type="text/javascript" src="html/jump.js"></script>
«Newer      Older»
Comment:
Name:

Back to home

Subscribe | Register | Login | N