用JS写一段判断是从搜索引擎搜索关键词来的移动端跳转到baidu.com的代码
时间: 2023-05-30 21:06:22 浏览: 396
```javascript
if (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent) && document.referrer.indexOf('https://www.baidu.com/s') === 0) {
window.location.href = 'https://www.baidu.com';
}
```
解释:
- `navigator.userAgent` 表示浏览器的 user agent 字符串,可以用来判断浏览器类型。这里使用正则表达式判断是否为移动设备。
- `document.referrer` 表示当前页面的来源 URL,这里判断是否来自百度搜索结果页。
- 如果是来自百度搜索结果页的移动设备,就重定向到百度首页。
相关问题
用JS写一段判断搜索引擎来路并且移动端跳转到baidu.com的代码
var referer = document.referrer;
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
if (referer.indexOf('google') > -1 || referer.indexOf('bing') > -1 || referer.indexOf('yahoo') > -1) {
if (isMobile) {
window.location.href = 'https://m.baidu.com';
} else {
window.location.href = 'https://www.baidu.com';
}
}
用JS写一段判断是从搜索引擎搜索关键词来的移动端用户跳转到taobao.com的代码,蜘蛛则不跳转
```js
var referrer = document.referrer; // 获取上一页的URL
var keywords = ['google', 'baidu', 'bing', 'sogou']; // 定义搜索引擎关键词
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent); // 判断是否为移动设备
// 判断是否为搜索引擎跳转
var isFromSearchEngine = keywords.some(function(keyword) {
return referrer.indexOf(keyword) > -1;
});
// 判断是否为移动设备且来自搜索引擎跳转
if (isMobile && isFromSearchEngine) {
location.href = 'https://www.taobao.com/';
}
```