如何在 Javascript 重定向中保留 URL 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8055791/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:07:00  来源:igfitidea点击:

How to keep URL parameters on Javascript redirect?

javascripturlredirectparameters

提问by Matt B

After clicking a dynamic link in an email, my users land on a page with a form pre-populated like this:

单击电子邮件中的动态链接后,我的用户会登陆一个页面,其中包含一个预先填充的表单,如下所示:

http://www.test.com/november_promo/default.html?Name=Test&Email=Test.com

http://www.test.com/november_promo/default.html?Name=Test&Email=Test.com

A script runs right at the beginning to re-direct them to a mobile version of the page:

一个脚本在开始时运行,将它们重定向到页面的移动版本:

<script type="text/javascript" src="redirection_mobile.min.js"></script>
<script type="text/javascript">
SA.redirection_mobile ({param:"isDefault", mobile_url: "www.test.com/november_promo/default_mobile.html", mobile_prefix : "http://", cookie_hours : "1" });
</script>

Is there any way to keep those parameters when they are redirected like that?

当它们像这样重定向时,有没有办法保留这些参数?

I know there are better ways to mobile-redirect with PHP, but my company's server is sketchy about letting my department's directory serve PHP files, so we try to do everything with Javascript.

我知道有更好的方法来使用 PHP 进行移动重定向,但是我公司的服务器对于让我部门的目录提供 PHP 文件很粗略,所以我们尝试使用 Javascript 做所有事情。

回答by vertazzar

document.location.search

can be used to append current location query strings to string in javascript

可用于将当前位置查询字符串附加到 javascript 中的字符串

e.g.

例如

<script>
var uri = 'foobar.html';

// while current location is 'baz.html?foo=1&a=b' for example

uri = uri + document.location.search;

// this will output 'foobar.html?foo=1&a=b'    
alert(uri);

</script>