英文:
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('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);
}
Using jQuery document ready function
$(document).ready(function() {
var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);
});
in order to use the jQuery library, you will need to include it
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Using DOMContentLoaded
event
document.addEventListener("DOMContentLoaded", function() {
var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);
});
Using setTimeout
:
setTimeout(function() {
var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);
}, 1000);
Using 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);
Using 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
});
Using 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
});
Using window.addEventListener
window.addEventListener("load", function() {
var maxConvLink = document.querySelector('a[data-mc-cta="1"]');
var mcLink = maxConvLink.getAttribute("href");
alert(mcLink);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论