写一段PC端不跳转,移动端用户通过搜索引擎访问跳转的JS代码
时间: 2024-05-10 11:21:35 浏览: 13
以下是一段PC端不跳转,移动端用户通过搜索引擎访问跳转的JS代码:
```javascript
// 判断是否为移动端
function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
// 判断是否为通过搜索引擎访问
function isFromSearchEngine() {
return document.referrer.indexOf('google') > -1 || document.referrer.indexOf('bing') > -1 || document.referrer.indexOf('baidu') > -1;
}
// 如果是移动端通过搜索引擎访问,则跳转到指定页面
if (isMobile() && isFromSearchEngine()) {
window.location.href = 'https://m.example.com';
}
```
以上代码通过判断用户的设备类型和访问来源,在移动端通过搜索引擎访问时自动跳转到移动端页面,提升用户体验。
相关问题
写一段移动端用户通过搜索引擎访问跳转的JS代码
我们可以使用以下JS代码来实现移动端用户通过搜索引擎访问跳转:
```
<script type="text/javascript">
var userAgent = navigator.userAgent.toLowerCase();
var mobileKeywords = ['iphone', 'android', 'windows phone', 'blackberry'];
var isMobile = false;
for (var i = 0; i < mobileKeywords.length; i++) {
if (userAgent.indexOf(mobileKeywords[i]) !== -1) {
isMobile = true;
break;
}
}
if (isMobile) {
window.location.href = 'http://m.example.com'; //跳转到移动端页面
} else {
window.location.href = 'http://www.example.com'; //跳转到PC端页面
}
</script>
```
这段代码会检测用户所使用的设备类型,如果是移动设备则跳转到移动端页面,否则跳转到PC端页面。其中,mobileKeywords数组存储了移动设备的关键字,通过遍历这个数组判断用户所使用的设备类型。如果包含移动设备关键字,则设置isMobile为true,执行相应的跳转操作。
用JS写一段判断是搜索引擎蜘蛛或用户访问的跳转代码
以下是一段示例代码,可以判断是否为搜索引擎蜘蛛或用户访问,并进行相应的跳转操作:
```javascript
// 判断是否为搜索引擎蜘蛛
function isSpider() {
const userAgent = navigator.userAgent.toLowerCase();
const spiders = [
"googlebot",
"bingbot",
"slurp",
"duckduckbot",
"yandexbot",
"sogou",
"exabot",
"facebot",
"ia_archiver",
"facebookexternalhit"
];
for (let i = 0; i < spiders.length; i++) {
if (userAgent.indexOf(spiders[i]) !== -1) {
return true;
}
}
return false;
}
// 跳转操作
function redirect(url) {
window.location.href = url;
}
// 判断并跳转
if (isSpider()) {
// 如果是搜索引擎蜘蛛,则跳转到网站地图
redirect("/sitemap.html");
} else {
// 如果是用户访问,则跳转到首页
redirect("/");
}
```
在上面的代码中,首先定义了一个 `isSpider` 函数,用于判断当前访问者是否为搜索引擎蜘蛛。该函数通过检查 `navigator.userAgent` 字符串中是否包含一些常见的蜘蛛标识来判断。
然后定义了一个 `redirect` 函数,用于进行跳转操作。最后,在主代码块中,根据 `isSpider` 函数的返回值来决定跳转到哪个页面。如果是搜索引擎蜘蛛,则跳转到网站地图页面;如果是用户访问,则跳转到首页。