HTML & JavaScript 重定向:如何保留 Referer 信息?
本文将详细介绍如何使用 HTML 和 JavaScript 进行页面重定向,并提供保留 Referer 信息的技巧,确保重定向后的页面能够获取到原页面地址。
HTML 重定向
<meta http-equiv='refresh' content='0;url=new_url' />
JavaScript 重定向
window.location.replace('new_url');
其中,new_url 为要重定向的 URL。
保留 Referer 信息
如果需要在重定向时保留 Referer 信息,可以在 URL 中添加 referer 参数,例如:
HTML 重定向
<meta http-equiv='refresh' content='0;url=new_url?referer=old_url' />
JavaScript 重定向
window.location.replace('new_url?referer=old_url');
其中,old_url 为重定向前的页面 URL。在重定向后的页面中可以通过获取 URL 中的 referer 参数来获取重定向前的页面 URL。
例如,在重定向后的页面中可以使用以下代码获取 Referer 信息:
const urlParams = new URLSearchParams(window.location.search);
const referer = urlParams.get('referer');
通过以上方法,您可以轻松实现页面重定向并保留 Referer 信息,以便于网站分析和用户行为追踪。
原文地址: http://www.cveoy.top/t/topic/jx2u 著作权归作者所有。请勿转载和采集!