Premature audio cutoffs in Javascript/CSS when using onmousedown event

One issue I'm facing is that a small beep sound should play when clicking on a link, but it gets cut off unless the click is held down.

<script>
  var beep = new Audio();
  beep.src = "audio/select.mp3";
  </script>
  <div class="frame">
    <div class="upperband">
  <img src="images\Upper bar.png" alt="Upper overlay">
</div>
  <div class="outer">
    <div class="header">Maddie's School Site_</div>
      <div class="wrapper">
        <nav>
        - home<br>
        <a href="made.html" onmousedown="beep.play()">- Stuff I Made<br></a>
        <a href="wrote.html" onmousedown="beep.play()">- Stuff I wrote<br></a>
        <a href="whothehell.html" onmousedown="beep.play()">- Who the hell am I?<br></a>
        </nav>
        <div class="right">
          <div class="shadowFX">
          <img src="images\Website_Sprite_ghost.svg" alt="avatar">
        </div>
        </div>
      </div>
    </div>
  </div>

I apologize if the code looks messy. I've only been in web programming school for a month and we haven't covered JavaScript yet, so my understanding is limited.

Answer №1

It appears that the issue stems from having a href attribute in the same element as the code that triggers the beep onmousedown.

    <a href="made.html" onmousedown="beep.play()">- Stuff I Made<br></a>
    <a href="wrote.html" onmousedown="beep.play()">- Stuff I wrote<br></a>

Essentially, when the user clicks on one of the above elements, the beep plays and then the user is redirected to a new page. However, loading a new page interrupts the beep from playing further.

To address this issue, there are several solutions based on what outcome you are looking for:

1.) If you want the beep to play only upon clicking the tag without redirecting, simply remove the href attribute like so:

    <a onmousedown="beep.play()">- Stuff I Made<br></a>

2.) If you wish for the beep to play first and then redirect the user after it finishes, utilize a timer function instead of href to control the redirection timing:

function handlePageChange() {
    beep.play(); // initiate beep
    
    setTimeout(function() {
        document.location.href = "made.html"; // redirect after beep
    }, 3000); // Adjust time for beep duration
}

3.) To have the beep play upon loading a new page, include beep.play() in the section of the desired page.

4.) For a seamless transition of the beep sound to the new page without delay, consider more advanced techniques such as those recommended here, though perfect audio continuity may not be achieved.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamic creation of HTML/Ionic checkbox leads to ng-change not binding properly

Recently, my team and I have been actively engaged in the process of handling an XML file and dynamically constructing a settings page based on the information extracted from it. Allow me to present an illustration of how these elements are dynamically cre ...

Is there a way to incorporate GIFs into a Node.js art generation script that anyone knows of?

I've been working on a node.js code that generates art by combining different layers using the PIL library. One of the layers I want to use is a gif, but for some reason, when I apply it, it shows up as a regular image even though the file is a gif. A ...

Establishing a color palette for various CSS classes using a gradient color spectrum

I am looking to create a gradient color scale and define classes for it based on a range of values. My scale ranges from 0 to 8.5, with increments of 0.25. This means I have a total of 34 different colors (8.5/0.25 = 34) to cover the entire spectrum. Each ...

I encountered difficulty displaying a list of documents in the view while working with derbyjs

Exploring derbyjs for the first time, unsure if I'm missing something or if there's a lack of documentation. My model is named "books" and I'm attempting to display a list of books. Here is my code snippet: module.exports = { propertie ...

Is it possible to delete an element from a JSON file by solely utilizing the element's ID?

I'm fairly new to JavaScript and I've tried various online solutions without success. Currently, I'm working on creating a backend for a todo list application where I aim to implement the feature to delete items from the list. Below is the ...

Webpack Error: SyntaxError - an unexpected token found =>

After transferring my project to a new machine, I encountered an error when running webpack --watch: C:\Users\joe_coolish\AppData\Roaming\npm\node_modules\webpack\bin\webpack.js:186 outputOption ...

The HTML form must be able to determine the number of records in the database

I am facing an issue with my HTML form written in PHP, where there are one or two checkboxes based on the user's selection from a dropdown earlier in the form. The challenge lies in determining how many records meet the query conditions in an SQL data ...

What is the best way to include a .AVI file in an HTML document?

I have come across some examples of .AVI in HTML on the internet. However, my page seems to be having issues. It displays fine in Chrome on my computer. But in Internet Explorer, there is a bar at the top and the videos appear blank afterwards. It ...

What is the best way to position multi-line text alongside an image without it wrapping below the image?

I managed to align a single line of text with an image successfully (view fiddle). <div class="profile-details-wrapper"> <img class="profile-picture" src="http://placehold.it/50x50" /> <span class="profile-details"> username &l ...

How can I attach events to newly generated elements without using jQuery?

If I want to dynamically add a form to my webpage (through AJAX or other JavaScript methods), how can I attach event listeners to these elements before they actually exist on the page? When using jQuery, it's simple to bind the events to a parent ele ...

Tips for generating an interactive collapse effect with the simple click of a button

Does anyone know how to implement a collapsible feature when a button is clicked? I would appreciate some assistance. Essentially, the goal is to extract data from the screen when the "Add" button is clicked, and then display it in a collapsible format. ...

When importing OrbitControls in main.js, THREE.js does not include the EventDispatcher export

Description I've been working on integrating the OrbitControls from THREE.js into my project. However, I encountered an error message in the Google Chrome DevTools console when trying to import the OrbitControls object. The requested module '. ...

Remove the click event once the sorting process has been completed

I am currently working on a project that involves creating a list of sortable images using jquery sortable. Users can easily drag and drop the images for sorting purposes. Additionally, each image's parent anchor has a click event attached to open it ...

Is there a more efficient method for incorporating artists' animations in ThreeJS?

As of now, I am dealing with a collada file that contains animations created by the artist. My goal is to control specific parts of the animation using buttons on my webpage. Do you think collada is the right format for this task, or should I consider us ...

Issue with Bootstrap NavBar collapsing properly on first element

I'm currently working on developing a responsive navbar. Everything seems to be functioning well in larger window sizes, but when I view it on an xs screen, the first element of the navbar does not collapse properly. For reference, here are two exampl ...

What's causing my Bootstrap cards to overlap on smaller screens?

When using 3 bootstrap cards in the following code, they are perfectly displayed side by side on large screens. However, on medium screens, the cards overlap, and on small screens, they completely overlap. I tried adjusting the class="col-lg-4 col-md- ...

"Utilizing the mongodb deleteOne function to remove an email within a document

In my collection, I have the following document model: { "user":{ "name":string, "email":string }, "address":{ "direction":string, "country":string }, "id":string } If you want to delete the document by id, there&apo ...

Creating a table that adapts to different screen sizes using

I'd like to set up a layout where an input element is next to a button, occupying the entire width of the container. Perhaps using a table would be the way to go. In my HTML and CSS setup, I have the following: .css-table { display: table; } ...

Passing data from a card component to a tab component in ReactJS

Just starting out with react and facing an issue. I want to transfer props from the child component to the parent component tabs that include favorite tabs. My plan was to pass the values through the handleClickOpen method where I click the favorites icon. ...

Is there a way to iterate through objects and add new properties to them?

I am trying to achieve the following object: let newPost = { title: "Post 1", Content: "New content" } with the code below: let newPost = {}; let postData = $(".post-data").each (function(index) { newPost.title = $ ...