如何通过保留orignal URL的URL参数执行Javascript重定向url?如何通过保留URL参数执行Javascript重定向url
如 原始地址:http://test.com?a=1&b=2
重定向到:http://sample.com?a=1&b=2
如何通过保留orignal URL的URL参数执行Javascript重定向url?如何通过保留URL参数执行Javascript重定向url
如 原始地址:http://test.com?a=1&b=2
重定向到:http://sample.com?a=1&b=2
下将获得当前的URL查询字符串:
var query = window.location.search;
,然后可以在重定向中使用:
window.location.replace('sample.com' + query);
更新
的.replace()方法将移除浏览器的历史记录当前的URL。如果你想保留当前的URL使用.assign()由@igor
提到修改位置对象:
location.href = location.href.replace (new RegExp("^" + "http://test.com"), "http://sample.com/");
的语句来替换从当前文档已加载的URL的开始。新网址的资源会自动加载。
您的示例网址不包含路径和片段部分(如在http://test.com/the/path/compon.ent?a=1&b=2#a_fragment
中)。他们是作为访问
location.pathname // 'http://test.com/the/path/compon.ent'
location.hash // '#a_fragment'
注意,这些URL组件发生建议明确谱写新的URL @MattSizzle的方式在他的回答概括。
或者'location.assign('sample.com'+ location.search)'如果重定向目标需要保存在浏览器历史记录中。 –