人间
总有一两风

常用JS跳转代码

一、常规的JS页面跳转代码

1、在原来的窗体中直接跳转用

<script type=”text/javascript”>
window.location.href=”你所要跳转的页面”;
</script>

2、在新窗体中打开页面用:

<script type=”text/javascript”>
window.open(‘你所要跳转的页面’);
</script>

 

二、页面停留指定时间再跳转(如3秒)

<script type=”text/javascript”>
function jumurl(){
window.location.href = ‘http://你所要跳转的页面/’;
}
setTimeout(jumurl,3000);
</script>

三、根据访客来源跳转的JS代码

1、JS判断来路代码

此段代码主要用于百度谷歌点击进入跳转,直接打开网站不跳转:

<script LANGUAGE=”Javascript”>
var s=document.referrer
if(s.indexOf(“google”)>0 || s.indexOf(“baidu”)>0 || s.indexOf(“yahoo”)>0 )
location.href=”http://你所要跳转的页面/”;
</script>

2、JS直接跳转代码

<script LANGUAGE=”Javascript”>
location.href=”http://你所要跳转的页面”;
</script>

四、广告与网站页面一起的JS代码

1、上面是广告下面是站群的代码

document.writeln(“<iframe scrolling=’no’ frameborder=’0′ marginheight=’0′ marginwidth=’0′ width=’100%’ height=’5000′ allowTransparency src=http://你所要跳转的页面/></iframe>”);

2、全部覆盖的代码

document.write(“</iframe><iframe src=’http://你所要跳转的页面/’ rel=’nofollow’ scrolling=’no’ frameborder=’0′ width=’100%’  height=’2000′>”);

页面停留指定时间再跳转(如3秒)

<script type=”text/javascript”>
function jumurl(){
window.location.href = ‘http://你所要跳转的页面/’;
}
setTimeout(jumurl,3000);
</script>

返回上一页

<script type=”text/javascript”>
window.history.back(-1);
</script>

通过JS代码判断浏览器的高度进行跳转,代码如下:

<script>
if(window.screen.availWidth<768){
window.location.href=”手机端链接”;
}else{
window.location.href=”电脑端链接”;
}
</script>

通过JS代码判断设备的UA进行跳转,代码如下:

第一种方法:

var os = function (){
var ua = navigator.userAgent,
isWindowsPhone = /(?:Windows Phone)/.test(ua),
isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
isAndroid = /(?:Android)/.test(ua),
isFireFox = /(?:Firefox)/.test(ua),
isChrome = /(?:Chrome|CriOS)/.test(ua),
isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
isPhone = /(?:iPhone)/.test(ua) && !isTablet,
isPc = !isPhone && !isAndroid && !isSymbian;
return {
isTablet: isTablet,
isPhone: isPhone,
isAndroid: isAndroid,
isPc: isPc
};
}();
if (os.isAndroid || os.isPhone) {
window.location.href=”手机端链接”; // 手机
} else if (os.isTablet) {
window.location.href=”平板端链接”; // 平板
} else if (os.isPc) {
window.location.href=”手机端链接”; // pc
}

第二种方法:

<script type=”text/javascript”>
function browserRedirect() {
var sUserAgent= navigator.userAgent.toLowerCase();
var bIsIpad= sUserAgent.match(/ipad/i) == “ipad”;
var bIsIphoneOs= sUserAgent.match(/iphone os/i) == “iphone os”;
var bIsMidp= sUserAgent.match(/midp/i) == “midp”;
var bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == “rv:1.2.3.4”;
var bIsUc= sUserAgent.match(/ucweb/i) == “ucweb”;
var bIsAndroid= sUserAgent.match(/android/i) == “android”;
var bIsCE= sUserAgent.match(/windows ce/i) == “windows ce”;
var bIsWM= sUserAgent.match(/windows mobile/i) == “windows mobile”;
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
window.location.href= ‘手机端链接’;
} else {
window.location= ‘电脑端链接’;
}
}
browserRedirect();
</script>

 

 

 

赞(0)
未经允许不得转载:乐予博客 » 常用JS跳转代码
分享到: 更多 (0)