unable to access the undefined resource

Currently working on building my personal portfolio using HTML, CSS, and JavaScript. I have implemented transition animations that work smoothly until clicking on another hyperlink triggers the transitions properly but results in a blank page with the error message 'cannot GET undefined'.

The index.html file consists of a simple home page with hyperlinks correctly linking to my CSS and JavaScript files.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="transitions.css">
    <title>Document</title>
</head>
<body>

        <div class="transition transition-1 is-active"></div>

        <section>

            <ul>
                <li><a href="/"><span>HOME</span></a></li>
                <li><a href="about.html"><span>ABOUT</span></a></li>
                <li><a href="contact.html"><span>CONTACT</span></a></li>
                <li><a href=""><span>GITHUB</span></a></li>
            </ul>


            <h1>Mateo Ghidini | Web Developer</h1>
        </section>
    

  <script src="main.js"></script>  

</body>

The transitions.css file:

.transition-1{
    position:fixed;
    top:0;
    left:0;
    right:0;
    bottom:0;
    z-index: 101;
    background-color: var(--dark);
    opacity: 0;
    pointer-events: none;
    transition: 0.5s ease-out;
}

.transition-1.is-active{
    opacity:1;
    pointer-events: all;
}


.transition-2{
    position:fixed;
    top:0;
    left:-100%;
    width:100%;
    bottom:0;
    z-index: 101;
    background-color: var(--dark);
    
    pointer-events: none;
    transition: 0.5s ease-out;
}

.transition-2.is-active{
    left:0px;
}


.transition-3{
    position:fixed;
    top:100%;
    left:0;
    right:0;
    height:100%;
    z-index: 101;
    background-color: var(--dark);
    
    pointer-events: none;
    transition: 0.5s ease-out;
}

.transition-3.is-active{
    top:0px;
}

Within main.js:

window.onload=() =>{
    
    const transition_el = document.querySelector('.transition');
    const anchors = document.querySelectorAll('a'); 
    setTimeout(()=>{
        transition_el.classList.remove('is-active');
    }, 500);

    for(let i = 0; i<anchors.length; i++){
        const anchor = anchors[i];

        anchor.addEventListener('click', e =>{
            e.preventDefault();
            let target = e.target.href;

            transition_el.classList.add('is-active');

            setTimeout(()=>{
                window.location.href = target;
            },500);
        });
    }
}

Seeking assistance in resolving this issue as it is unfamiliar territory for me.

Answer №1

The issue lies in the fact that when accessing e.target.href, the target element is actually a <span> tag rather than an <a> tag, which does not contain a href attribute.

To resolve this problem, one can eliminate the spans nested inside the links and utilize currentTarget instead of relying on target.

anchor.addEventListener('click', e => {
  e.preventDefault();
  let target = e.currentTarget.href;
  // etc...
});

Answer №2

The appropriate property to access the event argument is

e.currentTarget.href

main.js

window.onload = () => {
  const fadeOutElement = document.querySelector(".fadeOut");
  const links = document.querySelectorAll("a");
  setTimeout(() => {
    fadeOutElement.classList.remove("is-fading");
  }, 500);

  for (let i = 0; i < links.length; i++) {
    const link = links[i];
    link.addEventListener("click", (e) => {
      e.preventDefault();
      let targetUrl = e.currentTarget.href;

      fadeOutElement.classList.add("is-fading");

      setTimeout(() => {
        window.location.href = targetUrl;
      }, 500);
    });
  }
};

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

Unable to call a nested function within an onclick event function

I am facing an issue with a function that contains another function within it. The main function has an onclick event, and I want it to call the inner function. However, when I click on the element, it gives an error saying that the function is not defin ...

Tips on executing a $.get request while submitting a form?

Instead of using AJAX to submit my form, I am trying to implement a progress bar by making GET requests to the server while the form is submitting. This is particularly important when dealing with multiple file uploads that may cause the submission to take ...

Unable to assign a value to a variable in JavaScript

Today, I delved into the world of JavaScript and decided to test my skills by creating a page that changes images when clicking on a div. Everything worked perfectly until I wanted to add an input element to specify how many steps to jump each time the but ...

Data containing visual content is not being successfully transferred to the MySQL database

I am having an issue where the code is not sending data to the database after clicking on submit. I have also attempted using the POST method with no success. These are the table names in my database: id (AI), image, name, dob, fathername, fatheroccu ...

Encountering issues with reading undefined properties while working with react-chartjs-2 and chart js

Having trouble with react chartjs errors? Visit the link for more details https://i.stack.imgur.com/lI2EP.png The versions I'm using are ^3.5.0 for chart.js and ^4.0.1 for react-chartjs-2 Tried downgrading to version 2 but it didn't solve the ...

Is there an `insert_id` that can be used as an

I'm facing a simple issue here. I have a user creation page on my system where I display the newly created user's ID after creation. printf("New user has been added. The user ID is %d \n", $mysqli->insert_id ); My question is, how can I ...

What are the methods to determine if vue-router is being utilized within the present Vue instance?

I am working on creating a library that includes a vue.js component with navigation elements. The goal is for this component to be flexible, able to function both with and without vue router. When used with the router, it will utilize <router-link> ...

Implementing a jQuery selector feature in a headless browser like PhantomJS

I'm currently in the process of incorporating jQuery selector controls into a headless PhantomJS browser for testing purposes. Is this scenario even possible? Up until now, we've simply been bypassing the check to determine if the control exist ...

Creating Widgets with the help of CSS3 and HTML5

I am truly fascinated by the capabilities of HTML5 and CSS3 working together, and it's common knowledge that they require a browser to function. However, I'm curious if there is a way to create Desktop Widgets or Apps using HTML5, CSS3, and Java ...

Converting JSON data into an array of a particular type in Angular

My current challenge involves converting JSON data into an array of Recipe objects. Here is the response retrieved from the API: { "criteria": { "requirePictures": true, "q": null, "allowedIngredient": null, "excluded ...

How to center an image within a WordPress .php file

Could someone please assist with centering the image in the code snippet below? add_action('woocommerce_after_add_to_cart_button', 'add_content_on_add_to_cart_button'); function add_content_on_add_to_cart_button() { echo &qu ...

Tips for accessing and displaying JSON data using jQuery

Data in JSON format: [ { "ID":"25", "Serial":"1", "Purchase_id":"8", "Item":"23", "Unit":"1", "HSN":"84212120", "Quantity":"10", "Purchase_rate":"100", ...

Connecting RxJs to a React component: a step-by-step guide

Many examples demonstrating how to observe button clicks using RxJS typically follow this pattern: var button = document.querySelector('button'); Rx.Observable.fromEvent(button, 'click') .subscribe(() => console.log('Clicked! ...

Issue with uploading files on remote server using PHP

Below is the code I am currently using: <form action="index.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file"><br><br> <input type="submit" value="submit" name="submit"> </form> ...

Scrolling is disabled when using a full-screen background

My current issue involves a full screen background that is functioning properly when the window is resized, but it has caused a problem where scrolling is disabled when the page is longer than the window size. The content just gets cut off. Is there a way ...

Transform URL in AngularJS Service

An issue I'm facing with the app I'm developing is that users need to be able to change the IP address of the server where the REST API is hosted. The challenge lies in the fact that while the address is stored in a variable that can be changed, ...

Switch up the color of the flush list line break in Bootstrap 4

The code snippet taken from the documentation is as follows: <ul class="list-group list-group-flush"> <li class="list-group-item">Cras justo odio</li> <li class="list-group-item">Dapibus ac faci ...

Is my page not displaying correctly due to compatibility view?

My client "jordanzad.com" has a website where the main page design appears broken when viewed in compatibility view. Can you help fix this issue? ...

Updating and transitioning files at regular intervals using Jquery/Javascript

My goal is to create a web page that refreshes and loads different content files every 10 seconds without using an <iframe>. Currently, the files are displaying randomly instead of in order. I am not very familiar with Jquery and Javascript, but I wa ...

Verify whether a dependency is being passed into an Angular component

As mentioned in a discussion about IE caching issues, Internet Explorer tends to cache things when using ajax with AngularJS, causing potential problems. You can find a helpful solution to this problem here. Given that we have multiple angular apps on our ...