Scott C
Scott C

Reputation: 3

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.

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

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

Upvotes: 0

Views: 519

Answers (1)

Set Kyar Wa Lar
Set Kyar Wa Lar

Reputation: 4644

You can get the URL parameter like the following

window.location.search

And then you can redirect like the following

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

Upvotes: 1

Related Questions