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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题