You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

静态HTML站点如何获取联系表单的引荐页面URL并传入隐藏表单字段

解决静态站点获取引荐页面URL的问题

问题根源:你链接中的rel="nofollow noreferrer"是核心问题——noreferrer属性会强制浏览器不传递完整的引荐页面URL(部分浏览器仅返回域名,甚至清空referrer)。同时你之前的cookie方案逻辑错误:应该在用户点击链接的来源页面存储URL,而非在联系表单页面设置。

针对你的静态HTML站点(100+产品页),提供两个无需服务器端支持的可行方案:

方案1:自动给联系链接追加来源URL参数(推荐)

无需手动修改100+产品页的链接,用JS自动为所有指向联系页的链接追加当前页面URL作为参数,再在联系页读取参数填充隐藏字段。

产品页JS代码(所有产品页引入):

document.addEventListener('DOMContentLoaded', () => {
  // 匹配所有指向联系页的链接(替换为你的联系页路径)
  const contactLinks = document.querySelectorAll('a[href*="/contact-us.html"]');
  
  contactLinks.forEach(link => {
    const currentUrl = encodeURIComponent(window.location.href);
    // 处理已有参数的情况,避免重复添加?
    const separator = link.href.includes('?') ? '&' : '?';
    link.href += `${separator}from=${currentUrl}`;
    
    // 移除noreferrer(已通过参数传递来源,无需再阻止referrer)
    link.rel = link.rel.replace('noreferrer', '').trim();
  });
});

联系页JS代码:

document.addEventListener('DOMContentLoaded', () => {
  const urlParams = new URLSearchParams(window.location.search);
  const sourceUrl = urlParams.get('from');
  const hiddenField = document.querySelector('input[name="source_page"]');
  
  if (hiddenField && sourceUrl) {
    hiddenField.value = decodeURIComponent(sourceUrl);
  }
});

方案2:用SessionStorage传递来源URL

适合不想修改链接参数的场景,在用户点击链接时将当前页面URL存入SessionStorage,联系页读取后填充隐藏字段。

产品页JS代码:

document.addEventListener('DOMContentLoaded', () => {
  const contactLinks = document.querySelectorAll('a[href*="/contact-us.html"]');
  
  contactLinks.forEach(link => {
    link.addEventListener('click', () => {
      // 存入当前页面URL到SessionStorage
      sessionStorage.setItem('sourcePage', window.location.href);
      // 可选:移除noreferrer(同域下SessionStorage不受noreferrer影响,但移除后也不影响功能)
      link.rel = link.rel.replace('noreferrer', '').trim();
    });
  });
});

联系页JS代码:

document.addEventListener('DOMContentLoaded', () => {
  const sourceUrl = sessionStorage.getItem('sourcePage');
  const hiddenField = document.querySelector('input[name="source_page"]');
  
  if (hiddenField && sourceUrl) {
    hiddenField.value = sourceUrl;
    // 读取后清空,避免重复填充旧值
    sessionStorage.removeItem('sourcePage');
  }
});

注意事项:

  • 确保联系表单中存在隐藏字段:<input type="hidden" name="source_page" value="">
  • 若站点存在子域名,SessionStorage可能无法跨子域共享,此时方案1兼容性更好
  • 方案1的参数传递不受浏览器隐私设置影响,稳定性更高

内容的提问来源于stack exchange,提问作者JMGable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 16:46:12