将查询字符串添加到URL重定向

时间:2013-05-17 21:43:02

标签: javascript redirect

我正在尝试使用我可以成功执行的查询字符串,除非它是移动页面。我正在做的是检查它是否是移动和重定向。有没有办法将查询字符串附加到重定向网址?

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

1 个答案:

答案 0 :(得分:7)

我认为这可能就是你想要的......

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

我将其简化为window.location.search是一个空字符串或以?开头。同样适用于.hash

如果您的起始网址为http://www.some.com/entry?id=10,则重定向将为http://www.some.com/mobile?entry?id=10

哈希也是如此

http://www.some.com/entry?id=10#paragraph2 将重定向到 http://www.some.com/mobile?id=10#paragraph2

如果当前网址不包含,则重定向网址将保持不变。