javascript - If search contains "pc" search another site
问题描述
I want to check if the search contains a string "pc". If the search contains pc i want to redirect to another URL removing the term "pc" from the query and leaving the rest.
This should look like this:
www.somesite.com/?s=pc12121212 ----> sub.somesite.com/index.php?p=12121212
解决方案
function CheckAndMove(url){
const queries = url.split('?')[1]
if(!queries) return; // leave if you have no queries at all
const search = (new URLSearchParams(queries)).get('s') // extract the search query
if(!search || !search.indexOf('pc')==-1) return // leave if you have no search query or it doesn't contain pc
const id = search.replace('pc','') // remove the 'pc' part
window.location = 'https://www.sub.somesite.com/index.php?p='+id;
}
推荐阅读
- javascript - moment.js 无法将实际月份转换为其他格式
- android - 从 react-native-static-server 启动 StaticServer 时出错:未定义不是对象(评估“this.staticServer.start”)
- linux - sh shell 将子shell 重定向到文件,找不到正确的语法
- java - Playframework [Java]:通过代码填充选择选项
- tensorflow - 如何从张量流中的列表中制作所有“唯一”对?
- android - git remote add 命令:如何在 .git/config 文件中编辑远程标签?
- scala - 火花数据帧是否懒惰地加载镶木地板数据?
- php - 按帖子 ID 从帖子中删除标签和分类
- javascript - 如果我们只知道 className 的一部分,如何删除 className?
- javascript - Scrollreveal.js 使用 afterreveal 调用函数