Reputation: 3
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.
<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
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