-2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<img src="image/img1.jpg" alt="">
<script>
var img = window.document.getElementsByTagName("img")[0].src;
if (img.src == "image/img1.jpg")
{
console.log("É igual");
}
else {
console.log("Não é igual");
}
// console.log(img);
</script>
</body>
</html>
In the code above the if
compares if "image/img1.jpg"
is equal to "image/img1.jpg"
because it returns the false
that is Não é igual
, was not to return true
and execute É igual
?
var img = window.document.getElementsByTagName("img")[0].src;
you are already initiatingimg
with the value of the propertysrc
. Remove src here, or remove src inside if.– Andre
It did not work to remove src from the variable and if it runs Else the same way.
– Dom Domdomdom