douwei1944 2017-02-14 21:38
浏览 66
已采纳

如何在单击链接后保存href值

More specifically a client has asked that when their link on another site has been clicked, and the user is brought back to the clients site. Can the href value of the users clicked link be saved and used again in another function.

For ex. I clicked the clients logo on another site and it brought to the clients site. Now the content on the clients site is filtered based on where i came from.

I found the following which saves the href of a clicked link on current site.

    $(document).ready(function(){
  $('a').click(function() {
    if ($(this).attr('href')) someFunction.href = $(this).attr('href');
  });
});

function someFunction(a) {
  window.setTimeout("console.log(someFunction.href);",200);

But im not sure how to store that data and use it again.

  • 写回答

3条回答 默认 最新

  • dousou1967 2017-02-14 21:46
    关注

    If you have access to the "other" site, I'd recommend just attaching a query parameter to the link, such as: <a href="www.targetsite.com/?referrer=parentsite.com">Client site</a>. That's a very easy and dependable way to go at it.

    Another way would be server-side, using $_SERVER\['HTTP_REFERER'\]

    If you want to access it client-side, you can use document.referrer.

    Note that the last two (especially the client-side) are not as dependable, though you should generally be fine. Also note that document.referrer will be empty if the parent site is HTTPS and the target site is HTTP.

    Example

    jQuery(document).ready(function($){
      if(document.referrer === 'http://www.affiliatesite.com/product/myproduct')
          filterMyContent('affiliate')
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数