0

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>
asked Jul 27, 2019 at 14:08
1
  • 1
    developer.mozilla.org/en-US/docs/Web/API/… Take a look at window.location.search, and then use string interpolation to combine them together with your redirect URL Commented Jul 27, 2019 at 14:11

1 Answer 1

1

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;
answered Jul 27, 2019 at 14:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.