简体   繁体   English

单击链接后如何保存href值

[英]How can i save the href value after a link has been clicked

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. 用户单击的链接的href值是否可以保存并在其他功能中再次使用。

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. 我发现以下内容将点击链接的href保存在当前站点上。

    $(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. 但是我不确定如何存储该数据并再次使用它。

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> . 如果您有权访问“其他”网站,建议您仅将查询参数附加到链接,例如: <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'\\] 另一种方法是在服务器端使用$_SERVER\\['HTTP_REFERER'\\]

If you want to access it client-side, you can use document.referrer . 如果要在客户端访问它,可以使用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. 还要注意,如果父站点是HTTPS,而目标站点是HTTP,则document.referrer 将为空

Example

jQuery(document).ready(function($){
  if(document.referrer === 'http://www.affiliatesite.com/product/myproduct')
      filterMyContent('affiliate')
});

I think what you're looking for is query string parameters. 我认为您正在寻找的是查询字符串参数。 For example, if the link to your clients site that was clicked from elsewhere were myclientsite.com?from=google.com , you could parse that value (from=google.com) and use it to change content on your clients page. 例如,如果从其他位置单击的指向您的客户网站的链接是myclientsite.com?from=google.com ,则可以解析该值(from = google.com)并使用它来更改客户页面上的内容。 See https://davidwalsh.name/query-string-javascript for details on parsing these values with javascript. 有关使用javascript解析这些值的详细信息,请参见https://davidwalsh.name/query-string-javascript This only works with the caveat that you can control the url of the link on the external site. 这仅适用于警告,您可以控制外部站点上的链接的URL。

If you are able to set the link on the other site you could add a query string to the URL eg clientsite.com?referrer=othersite.com . 如果您可以在其他站点上设置链接,则可以在URL中添加查询字符串,例如, clientsite.com?referrer=othersite.com Then change the site based on the referrer. 然后根据引荐来源网址更改站点。

If you can't change the link on the other site you could try see where the user has come from using the referrer (although this isn't always very reliable) 如果您无法更改其他站点上的链接,则可以尝试查看用户来自使用引荐来源网址的位置(尽管这并不总是很可靠)

//Javascript
var referrer = document.referrer;

//PHP
$referrer = $_SERVER['HTTP_REFERER'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM