vendredi 25 février 2022

random link on button press keeps concatenating

(sorry if the title sucks i spent like 30 minutes trying to word i physicaly cant like what do you put)

hey guys so i have this code here and the goal is that i can generate a random newgrounds song every time the generate button is clicked. i can get it working without noticeable flaws (unless you click the scroll wheel to make a new tab in which case the illusion will fail) but the problem is that i wanted it to work without needing a refresh. so i made the button create a new tab instead but now it appends the random number and ive ran out of question i can ask google so can someone either help me fix the fact it appends the number to the new link and not the base link or help me make the goal of a random newgrounds link generator in a more efficient way? if anything here looks bad then just know i am a beginner when it comes to html and javascript i spent most of my coding life on scratch. if some of the code looks good then i probably copied it

<!DOCTYPE html>
<html> 

<head> 
<link rel="stylesheet" href="CSS1.css">

<script>
  window.onclick = () => {
    document.querySelectorAll("a").forEach(link =>
     link.setAttribute("href", link.getAttribute("href") + Math.floor(Math.random() * 111392.8) + 1)
    );
  };
</script>

</head>

<title>
Newgrounds Song Generator
</title> 

<body>

<h1>Click the button to visit a random song!</h1>

<a href="https://www.newgrounds.com/audio/listen/" target="_blank">
<button>
Generate
</button>
</a>

</body>

</html>



Aucun commentaire:

Enregistrer un commentaire