分享一些常见的JS跳转代码

40次阅读
2024年08月15日 15:59:48

共计 2663 个字符,预计需要花费 7 分钟才能阅读完成。

常规的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=https://你所要跳转的页面/></iframe>”);

2、全部覆盖的代码

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

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

<script type=”text/javascript”>
function jumurl(){
window.location.href = ‘https://你所要跳转的页面/’;
}
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
鹿泽
版权声明:本站原创文章,本文由 鹿泽 于 2024年8月15日 15:59:48 发表,共计 2663 字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。
如若本站内容侵犯了原著者的合法权益,可联系我们邮箱:526009505@qq.com进行处理。
最新文章
在表格中使用谷歌翻译函数公式批量翻译

在表格中使用谷歌翻译函数公式批量翻译

不用写VBA,在Excel或WPS表格中,使用谷歌翻译函数GoogleTranslate(),写写公式就可以快...
需求与满足度的相关提高权重算法原理

需求与满足度的相关提高权重算法原理

网站权重是什么,权重就代表一个网站的得分级别,权重越高排名能力就越强,同时可获得的关键词数量就越多。比如大家经...
seo绩效考核指标标准是什么?

seo绩效考核指标标准是什么?

优化人员的seo绩效考核标准是比较难制定的,在于很多地方无法衡量,尤其是外包公司,除了要做客户的运营之外,还要...
seo企业优化顾问如何针对网站做SEO分析?

seo企业优化顾问如何针对网站做SEO分析?

不过分的说,分析网站几乎每一个SEO站长都必须掌握的技能,因为只有学会分析网站,才能找出细微的问题,才能有所成...
网站怎么优化排名?一些平时积累的SEO收录知识总结

网站怎么优化排名?一些平时积累的SEO收录知识总结

经常有站长在优化网站的过程中不知所措,本人把一些平时积累的SEO收录知识总结成相关文章整理成笔记分享给大家,希...