【问题标题】:jQuery: Dynamically passing UTM parameters to a link in the pagejQuery:动态地将 UTM 参数传递给页面中的链接
【发布时间】:2014-07-18 15:16:40
【问题描述】:

我希望使用 jQuery 在我的页面中动态附加一个链接,其中包含来自当前 URL 的 UTM 信息。

例如:

我当前的页面 URL 可以有许多不同的 UTM 参数,但我想解析出“源”和“内容”之间的信息。然后我想将这些参数传递给页面中给出以下参数的链接。

当前页面网址为:

www.current-page.com/?Campaign=Test&Medium=Email&Source=My%Newsletter&Content=&Term=

号召性用语按钮/链接默认指向以下内容:

www.next-page.com?source=website

我想解析出当前 URL 的 My%20Newsletter 部分,然后将按钮/链接 href 从 source=website 更改为 source=My%20Newsletter强>。

我有下面的代码,但它仍然需要一些工作。

var tracking_param = "source=My%20Newsletter";
$('a.my-class').each(function() {
  var param_linker;
  var href = $(this).attr('href')
  if(href !== undefined) {
  if(href.indexOf('?') >=0) { 
    param_linker = "&"
  }
  else {
    param_linker = "?"
  }
  var final_url = href + param_linker + tracking_param;
  $(this).attr('href', final_url);
  }
});

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: jquery html parsing hyperlink


    【解决方案1】:

    我认为这应该适合你:

    $('a.my-class').each(function() {
        var href = $(this).attr('href')
        if(href !== undefined) {
            var url = document.URL;
            int index = url.indexOf("Source=");
            if(index != -1){
                url = url.substr(index + 7) // 7 = length of "Source="
                indexx = url.indexOf("&");
                if(indexx != -1)
                    url = url.substr(0, indexx + 1);
                // now url contains the value
                href = href.replace("website", url);
                $(this).attr('href', href);
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多