Hi @Souleymane Ben Daouda Dieye
My design is to judge whether a link is valid based on the Fetch API. The Fetch API is a JavaScript API that lets us send a web request and process it after receiving the response. It can be used to request data from a server, upload data, or send other types of requests.
And the CSS method you give is not flexible to judge. So I use EventListener and the mouseOver, mouseOut events to implement your previous CSS functionality.
Valid URLs are red, invalid URLs are yellow.
The Fetch API will cause Cors issues, so add mode: 'no-cors' to your code.
<form id="form1" runat="server">
<div>
<ul class="navbar-nav">
<li>
<a id="demo" href="https://localhost:44371/Defa2ult.aspx">link</a>
</li>
</ul>
<script>
function isValidURL(url) {
return fetch(url, { mode: 'no-cors' })
.then(function (response) {
if (response.ok || response.status >= 200 && response.status < 300) {
alert("success");
document.getElementById("demo").addEventListener("mouseover", mouseOver);
document.getElementById("demo").addEventListener("mouseout", mouseOut);
function mouseOver() {
document.getElementById("demo").style.color = "red";
}
function mouseOut() {
document.getElementById("demo").style.color = "black";
}
return true;
} else {
alert("fail");
document.getElementById("demo").addEventListener("mouseover", mouseOver);
document.getElementById("demo").addEventListener("mouseout", mouseOut);
function mouseOver() {
document.getElementById("demo").style.color = "yellow";
}
function mouseOut() {
document.getElementById("demo").style.color = "black";
}
return false;
}
})
.catch(function (error) {
return false;
});
}
</script>
<script >
isValidURL(document.getElementById("demo"));
</script>
</div>
</form>
Best regards,
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.