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>
-
1developer.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 URLAHB– AHB2019年07月27日 14:11:56 +00:00Commented Jul 27, 2019 at 14:11
1 Answer 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
lang-js