javascript 将查询字符串添加到 URL 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16618354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add query string to URL redirect
提问by shinjuo
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>
回答by Morgan ARR Allen
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
我将其简化为 window.location.search 要么是一个空白字符串,要么以 ? 开头。.hash 也一样
If your starting URL was http://www.some.com/entry?id=10the redirect would be http://www.some.com/mobile?entry?id=10
如果您的起始 URL 是http://www.some.com/entry?id=10,则重定向将是http://www.some.com/mobile?entry?id=10
Same goes for hashes
哈希也一样
http://www.some.com/entry?id=10#paragraph2would redirect to http://www.some.com/mobile?id=10#paragraph2
http://www.some.com/entry?id=10#paragraph2将重定向到 http://www.some.com/mobile?id=10#paragraph2
If the current URL does not contain either the redirect URL will remain as is.
如果当前 URL 不包含任何一个重定向 URL,则重定向 URL 将保持原样。