首页
/ JS Mobile Redirection 技术文档

JS Mobile Redirection 技术文档

2024-12-20 13:06:22作者:余洋婵Anita

本文档将详细介绍如何安装和使用 JS Mobile Redirection 脚本,以及如何通过 API 进行配置和操作。

1. 安装指南

首先,确保你有一个 JavaScript 环境可用。以下是将 JS Mobile Redirection 脚本添加到你的项目中的步骤:

  1. 从 GitHub 下载 redirection_mobile.js 文件。
  2. 将下载的 redirection_mobile.js 文件放入你的桌面网站版本中。

2. 项目使用说明

在页面上包含 redirection_mobile.js 文件后,你需要调用 SA.redirection_mobile() 函数以启动重定向功能。以下是如何在 HTML 中实现这一点的示例:

<!doctype html>
<html>
<head>
    <script src="redirection_mobile.js"></script>
    <script>
        SA.redirection_mobile();
    </script>
</head>

3. 项目 API 使用文档

SA.redirection_mobile() 函数接受多个配置参数,以下是对每个参数的详细说明:

  • mobile_prefix: 添加到主机名的前缀。例如,"m" 将重定向到 "m.domain.com"。
  • mobile_url: 用于重定向的移动 URL(不包括协议)。
  • mobile_scheme: 移动网站域的 URL 协议(如 "http" 或 "https")。
  • redirection_param: 避免重定向的查询字符串参数。
  • cookie_hours: 重定向到桌面网站后 cookie 存在的小时数。
  • tablet_redirection: 启用/禁用对平板电脑的重定向。
  • tablet_host: 用于平板电脑重定向的主机名。
  • keep_path: 是否在目标 URL 中保留原始 URL 的路径。
  • keep_query: 是否在目标 URL 中保留原始 URL 的查询字符串。
  • beforeredirection_callback: 在重定向之前调用的回调函数。
  • append_referrer: 是否将 document.referrer 添加到目标 URL。
  • append_referrer_key: 用于 document.referrer 的键。

以下是一些配置示例:

SA.redirection_mobile({
    mobile_prefix: "mobile",
    cookie_hours: "2"
});

SA.redirection_mobile({
    mobile_url: "whatever.com/example",
    mobile_prefix: "https"
});

SA.redirection_mobile({
    tablet_redirection: "true",
    mobile_url: "whatever.com/example",
    mobile_prefix: "https"
});

SA.redirection_mobile({
    before_redirection_callback: function() {
        alert("!");
        return false;
    }
});

4. 项目安装方式

除了使用 redirection_mobile.js 文件,还可以使用 redirection_mobile_self.js,这是一个自执行的匿名函数,使用默认值。只需将 redirection_mobile_self.js 文件放到你的服务器上,并在 HTML 中调用该脚本:

<!doctype html>
<html>
<head>
    <script src="redirection_mobile_self.js"></script>
</head>

此外,还有一个 redirection_mobile_testable.js 文件,它是 redirection_mobile.js 的一个副本,但为了测试目的,它使用了 documentwindownavigator 等参数。

登录后查看全文
热门项目推荐