Javascript在href链接点击返回数字

时间:2018-04-12 15:26:41

标签: javascript

我如何返回价值' 170236'点击'查询'在这个例子中?

<a class="mylink" 
href="https://www.mydomain.co.uk/help/contact/170236">Enquire</a>

我需要将其用于Google跟踪代码管理器中的变量

2 个答案:

答案 0 :(得分:0)

function getLastSegment()
{
  var links = document.getElementsByClassName('mylink');
  var link = links[0].href;
  alert(link.substr(link.lastIndexOf('/') + 1));
}
<a class="mylink" 
href="https://www.mydomain.co.uk/help/contact/170236" onClick="getLastSegment()">Enquire</a>

答案 1 :(得分:0)

知道了!

function() {

var links = document.getElementsByClassName('mylink test tester');
var link = links[0].href;
var element = link.substr(link.lastIndexOf('/') + 1);

return element;

}