【问题标题】:How to capture URL parameters and pass to a url redirect如何捕获 URL 参数并传递给 url 重定向
【发布时间】:2019-07-27 22:32:49
【问题描述】:

我正在为付费搜索设置一个页面(仅限移动设备),我想在其中检测用户的操作系统,并在页面加载时将他们重定向到基于操作系统的另一个链接。我的检测和重定向工作良好,但我希望能够从引用源中获取 URL 参数,并附加到重定向 URL。

我基本上希望能够从“?”中获取所有内容。覆盖并附加到下面示例中的 url。

https://mysubdomain.myurl.com/landingpage?pid=google_lp&utm_medium=adwords&utm_campaign=%7Bcampaign%7D&utm_source=%7Badgroup%7D&utm_content=354646179808&utm_term=medical%20team

<script>
function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}</script>

<script>
function DetectAndServe(){
    let os = getMobileOperatingSystem();
    if (os == "Android") {
        window.location.href = "https://myurl.com[APPENDED PARAMETERS HERE]"; 
    } else if (os == "iOS") {
        window.location.href = "https://myurl.com[APPENDED PARAMETERS HERE]";
    } else {
        window.location.href = "https://myurl.com[APPENDED PARAMETERS HERE]";
    }
}
</script>

【问题讨论】:

标签: javascript


【解决方案1】:

你可以像下面这样获取URL参数

window.location.search

然后你可以像下面这样重定向

window.location.href = "https://myurl.com" + window.location.search;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 2015-10-14
    • 2021-03-26
    • 2018-08-24
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多