I'm currently working on a script that reads a text file and generates an HTML link using a temporary email address.
<script>
//<![CDATA[
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementsByClassName('temporary_email')[0].innerHTML = "<a href=\"mailto:" + xhttp.responseText + "\">Email</a>";
}
};
xhttp.open("GET", "/temporary_email.txt", true);
xhttp.send();
//]]>
</script>
Everything is functioning as planned, allowing me to insert the
<span class="temporary_email"></span>
code snippet anywhere for the link to be displayed.
The issue: I've noticed that I am only able to fetch the email address once. If I have a mailto:
link in the body and another one in the footer, the script fails to work. This leads me to believe that there may be something wrong with the way I am handling variables since I am not very experienced with JavaScript yet.
PS: I am attempting to avoid using jQuery. I have tried several simple solutions like duplicating the script and giving it a different name for document.getElementsByClassName
, but without success. Essentially, I am looking for a quick and basic workaround until I gain enough knowledge in JavaScript to resolve this issue properly.