使用Javascript从ahref获取URL

huangapple go评论61阅读模式
英文:

Get URL from ahref using Javascript

问题

In this above a href code, a random URL will be generated by advertising network javascript placed before </head> tag.

我在上述a href代码中,广告网络的JavaScript将在</head>标签之前生成一个随机URL。

I am trying to get the random URL value using JS code:

我正在尝试使用JS代码获取随机URL的值:

var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);	

This code is placed before </body> tag. The problem is this above JS code getting value #url instead of real random URL value.

这段代码放置在</body>标签之前。问题是,上面的JS代码获取的值是#url,而不是真正的随机URL值。

As far I know, Javascript work in sequence, so, first Ad network JS code will place url to a href tag and than my code will get the value but it not working as I think.

据我所知,JavaScript按顺序执行,因此,首先广告网络的JS代码将URL放入a href标签,然后我的代码将获取该值,但它不按我所想的方式工作。

英文:

<a href="#url" data-mc-cta="1" style="display:none">Link</a>

In this above a href code, a random URL will be generated by advertising network javascript placed before </head> tag

I am trying to get the random URL value using JS code :

var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);	

This code is placed before </body> tag. The problem is this above JS code getting value #url instead of real random URL value.

As far i know, Javascript work in sequence, so, first Ad network JS code will place url to a href tag and than my code will get the value but it not working as I think.

答案1

得分: 1

使用window.onload

window.onload = function() {
  var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
  var mcLink = maxConvLink.getAttribute("href");
  alert(mcLink);	
}

使用jQuery文档准备函数

$(document).ready(function() {
  var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
  var mcLink = maxConvLink.getAttribute("href");
  alert(mcLink);    
});

要使用jQuery库,您需要包括它:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

使用DOMContentLoaded事件

document.addEventListener("DOMContentLoaded", function() {
  var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
  var mcLink = maxConvLink.getAttribute("href");
  alert(mcLink);
});

使用setTimeout

setTimeout(function() {
  var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
  var mcLink = maxConvLink.getAttribute("href");
  alert(mcLink);
}, 1000);

使用setInterval

var intervalId = setInterval(function() {
  var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
  if (maxConvLink) {
    clearInterval(intervalId);
    var mcLink = maxConvLink.getAttribute("href");
    alert(mcLink);
  }
}, 1000);

使用MutationObserver

var targetNode = document.querySelector('a[data-mc-cta="1"]');
    
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === "attributes" && mutation.attributeName === "href") {
      var maxConvLink = mutation.target;
      var mcLink = maxConvLink.getAttribute("href");
      alert(mcLink);
    }
  });
});
    
observer.observe(targetNode, {
  attributes: true
});

使用IntersectionObserver

var targetNode = document.querySelector('a[data-mc-cta="1"]');
    
var observer = new IntersectionObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if ( mutation.type === "attributes" && mutation.attributeName === "href") {
      var maxConvLink = mutation.target;
      var mcLink = maxConvLink.getAttribute("href");
      alert(mcLink);
    }
  });
});
    
observer.observe(targetNode, {
  attributes: true
});

使用window.addEventListener

window.addEventListener("load", function() {
  var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
  var mcLink = maxConvLink.getAttribute("href");
  alert(mcLink);	
});
英文:

There are different approaches to achieve the output. Choose the one that best suits your needs.

Using Window.onload

window.onload = function() {
  var maxConvLink = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);
  var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
  alert(mcLink);	
}

Using jQuery document ready function

$(document).ready(function() {
  var maxConvLink = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);
  var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
  alert(mcLink);    
});

in order to use the jQuery library, you will need to include it

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;

Using DOMContentLoaded event

document.addEventListener(&quot;DOMContentLoaded&quot;, function() {
  var maxConvLink = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);
  var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
  alert(mcLink);
});

Using setTimeout:

setTimeout(function() {
  var maxConvLink = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);
  var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
  alert(mcLink);
}, 1000);

Using setInterval:

var intervalId = setInterval(function() {
  var maxConvLink = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);
  if (maxConvLink) {
    clearInterval(intervalId);
    var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
    alert(mcLink);
  }
}, 1000);

Using MutationObserver

var targetNode = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === &quot;attributes&quot; &amp;&amp; mutation.attributeName === &quot;href&quot;) {
      var maxConvLink = mutation.target;
      var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
      alert(mcLink);
    }
  });
});

observer.observe(targetNode, {
  attributes: true
});

Using IntersectionObserver

var targetNode = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);

var observer = new IntersectionObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === &quot;attributes&quot; &amp;&amp; mutation.attributeName === &quot;href&quot;) {
      var maxConvLink = mutation.target;
      var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
      alert(mcLink);
    }
  });
});

observer.observe(targetNode, {
  attributes: true
});

Using window.addEventListener

window.addEventListener(&quot;load&quot;, function() {
  var maxConvLink = document.querySelector(&#39;a[data-mc-cta=&quot;1&quot;]&#39;);
  var mcLink = maxConvLink.getAttribute(&quot;href&quot;);
  alert(mcLink);	
});

huangapple
  • 本文由 发表于 2023年3月31日 18:54:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定