簡體   English   中英

如何捕獲URL參數並傳遞給URL重定向

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

我正在建立一個付費搜索頁面(僅限移動設備),我想在該頁面上檢測用戶的操作系統,並在頁面加載時根據操作系統將其重定向到另一個鏈接。 我的檢測和重定向工作正常,但是我希望能夠從引用源獲取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>

您可以獲取如下所示的URL參數

window.location.search

然后您可以像下面這樣重定向

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM