5

I am trying to use a query string which I can successfully do unless it is a mobile page. What I am doing is checking if it is mobile and redirecting. Is there a way to attach the query string to the redirect url?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript">// <![CDATA[  
    var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
    if (mobile) {  
        document.location = "http://www.xxxxxxx.com/mobile";  
    }  
// ]]></script> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>xxxxxxxxx</title>

<script type="text/javascript">
    window.onload = function(){
        var total = 0;
        for(var x = 0; x < parameters.length; x++){
            document.getElementById("customerID").value = parameters[x].value;
        }
        document.forms[0].onsubmit = validate;  
    }
</script>
6
  • 2
    What if some mobile device you didn't mention there navigates your site? I think you are approaching the problem wrong. Commented May 17, 2013 at 21:44
  • How about: + window.location.search ? Commented May 17, 2013 at 21:46
  • just add the query string to the URL , whats the issue? Commented May 17, 2013 at 21:47
  • 1
    Would'nt the serverside be a more proper place to do those kind of redirects ? Commented May 17, 2013 at 21:47
  • You will never capture all the devices. Consider why you need to treat mobile differently and test for that instead. E.g. test for screen size using css media queries or javascript and adjust layout based on that. Try to avoid using redirects which slow down load times on what is already likely to be a slow connection Commented May 17, 2013 at 21:54

1 Answer 1

9

I think this might be what you want..

var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
if (mobile) {
  window.location = "http://www.xxxxxxx.com/mobile" + window.location.search + window.location.hash;
}

I simplified this as window.location.search is either a blank string or starts with ?. Same goes for .hash

If your starting URL was http://www.some.com/entry?id=10 the redirect would be http://www.some.com/mobile?entry?id=10

Same goes for hashes

http://www.some.com/entry?id=10#paragraph2 would redirect to http://www.some.com/mobile?id=10#paragraph2

If the current URL does not contain either the redirect URL will remain as is.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.