简体   繁体   English

如何捕获URL参数并传递给URL重定向

[英]How to capture URL parameters and pass to a url redirect

I am working on setting up a page for paid search (mobile only) where I want to detect the user's OS and redirect them to another link based on the OS upon page load. 我正在建立一个付费搜索页面(仅限移动设备),我想在该页面上检测用户的操作系统,并在页面加载时根据操作系统将其重定向到另一个链接。 I have the detection and redirection working well, but I want to be able to grab the URL parameters from the referring source, and append to the redirect URL. 我的检测和重定向工作正常,但是我希望能够从引用源获取URL参数,并将其附加到重定向URL。

I basically want to be able to grab everything from the "?" 我基本上希望能够从“?”中获取所有内容。 over and append to the url in the example below. 在下面的示例中,覆盖并附加到该网址。

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 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>

You can get the URL parameter like the following 您可以获取如下所示的URL参数

window.location.search

And then you can redirect like the following 然后您可以像下面这样重定向

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM