I need to show a DIV element when a specific anchor is in an active state within an HTML document

Is there a way to make the code below only visible when the url ends with #404?

<!--404 code-->
<style>
  .div1 {
    width: 300px;
    height: 100px;
    border: 1px solid blue;
    border-color: #ff0263;
    box-sizing: border-box;
  }
</style>

<body>
  <font face="century gothic">
    <div class="div1" name="div1">
      <span id='close' style="cursor:pointer" onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span> The vite you were looking for could not be found. <a href="/404-what-to-do"><button class="button button5">What do I do now?</button></a></div>
  </font>
  <script>
    window.onload = function() {
      document.getElementById('close').onclick = function() {
        this.parentNode.parentNode.parentNode
          .removeChild(this.parentNode.parentNode);
        return false;
      };
    };
  </script>
  <style>
    #close {
      float: right;
      display: inline-block;
      padding: 2px 5px;
      background: #ccc;
    }
    
    #close:hover {
      float: right;
      display: inline-block;
      padding: 2px 5px;
      background: #ccc;
      color: #fff;
    }
  </style>
  <script>
    $(document).ready(function() {
      $("#id1").click(function() {
        $(".div1").css('display', 'block');
      });
    });
  </script>

I am the creator of Vite.website, and I want to ensure that the code above is only visible when the URL has #404. How can I achieve this functionality on my website? Thank you!

Answer №1

Although this primarily involves using CSS, incorporating jQuery is also an option. One could extract the hash value of the current page URL and implement conditional statements to achieve their desired outcome.

Answer №2

If you want to retrieve the hash location of a webpage, you can utilize:

window.location.hash

One possible solution, especially if you are working with jQuery, could be:

function checkHashFor404() {
    var hash = window.location.hash.slice(-1);
    if (hash === "404") {
        // Display content for 404 error
        // $(selector).addClass(classname); recommended
    } else {
        // The hash does not indicate a 404 error
        // $(selector).removeClass(classname); recommended
    }
}

Then, you can trigger this function by binding it to window.onload:

window.onload = checkHashFor404;

For displaying specific content based on the condition, use the provided if statement and add a link rel='stylesheet' using:

$("head").append("<link rel='stylesheet' href='custom-styles.css');

I hope this guidance assists you in resolving your query!

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

An error has occurred: Noty (notification library) is not defined in this AngularJS Web Application

I am currently diving into the world of AngularJS and building a web application from scratch. As a newbie to AngularJS, I want to point out that I might be missing something crucial. An issue has arisen: After installing the Noty library (npm install no ...

Once the timer has finished, I would like for a specific component to be displayed

I am trying to make my component CountDownSquare disappear once the timer has fully counted down. In my homePageData, I have the main text stored in a h3 element that should only appear once the countdown is complete. I attempted to use a ternary stateme ...

Is it possible to incorporate FCM into Next.js and effectively handle server-side events for FCM on Next.js servers?

Is it feasible to incorporate the Firebase Cloud Messaging (FCM) into my upcoming Next.js application? Can I manage both client-side and server-side modules within the server side? ...

Efficiently Aligning Elements in CSS both Vertically and Horizontally

I'm struggling to align my form both vertically and horizontally. It's okay if this doesn't work on IE - I will include an IE detection script to prompt users to switch browsers. However, Firefox and Chrome support is essential. Below is m ...

Taking a Breather with mywindow.print()

I'm currently utilizing a fantastic printing script that I found: <script type="text/javascript"> function PrintElem(elem) { Popup($(elem).text()); } function Popup(data) { var mywindow = window.ope ...

In Angular, when a component is clicked, it is selecting entire arrays instead of just a single item at a

I am currently working on implementing a rating feature using Angular. This component will be used to rate different languages based on how proficient I am with them. My issue lies in the fact that when I click on one array element, it automatically selec ...

Tips for validating form input upon submission in Angular 6

Within my Angular application, I have successfully implemented form validators. However, I am aiming to trigger form validation specifically upon submission. This means that when the user clicks on the submit button and the form is invalid, the errors indi ...

increasing the size of the JSON object

I have a function that is being called multiple times utilizing jQuery to fetch different JSON data from an API. My aim is to calculate the total count of a specific portion of the JSON retrieved. Below is an example of what I currently have: getTheData( ...

Inquiring about paint-order support for HTML elements using @supports

For those looking to create boldly stroked texts in Firefox and Safari, using paint-order and text-stroke (or the prefixed -webkit-text-stroke) is the way to go. Here's an example: h1 { -webkit-text-stroke: 5px red; paint-order: stroke fil ...

Managing automatic page redirects in Ajax requests

For the past two days, I have been encountering a challenging issue with my Laravel application that is hosted on Heroku server. The app allows file uploads through an ajax request, displaying upload progress on the page and returning JSON response upon co ...

Changing the State of a CheckBox with ReactJS and Material UI

Utilizing Material UI's Checkbox, I am creating a form to input data and update or add values into a state object. This form serves the purpose of both editing an existing holiday or adding a new one. I'm currently facing an issue where the stat ...

Typescript error: The property 'set' is not found on type '{}'

Below is the code snippet from my store.tsx file: let store = {}; const globalStore = {}; globalStore.set = (key: string, value: string) => { store = { ...store, [key]: value }; } globalStore.get = (key) => { return store[key]; } export d ...

Activate outline styling for Bootstrap radio buttons when clicked

I was in the midst of developing my bootstrap application. Every time I click on a radio button, there is a noticeable blue outline as shown here: https://i.sstatic.net/Uzm7b.png I attempted to address this issue using the following CSS code: .form-chec ...

How can I store the status of checked and unchecked checkboxes in an array of objects using Angular 7?

I have a set of checkboxes with a parent-child structure, and their values are generated dynamically in a loop. When I click the submit button, I want to capture the selected or unselected values in the specified format (as shown in the commented output) ...

Could someone lend a hand in getting the X to align properly on this div? I've been struggling with it for ages and just can't seem to crack

This is a demonstration of the CodePen project: Click here to view <div class="annotation"> <div class="annotext"> <div class=annobar></div> <div class="x">✕</div> Lorem ipsum dolor sit amet, consectetur adipiscing e ...

Reveal and Conceal, the ever-changing show

As I work on my blog, I want to make the layout more compact by having a link that reveals comments and entry forms when clicked. I've seen this feature on other sites as "Comments (5)", but I'm unsure how to implement it myself. Below is a snip ...

Unable to retrieve information from the wiki API

Here is the link to my codepen project: https://codepen.io/mlestina/pen/OZpOQW?editors=1111 I am having trouble retrieving data from the Wiki API. When I check the Contr-Shift-J readout, I see: Loading failed for the <script> with source “https: ...

Trick to bypass inline script restrictions on Chrome extension

I have developed a Chrome extension with a feature that involves looping a list of links into a .div element. Here is the code snippet: function updateIcd() { modalIcdLinks.innerHTML = ''; let icdLinks = ''; for (let i = 0; i < icd ...

The drop-down links vanish into the depths of the webpage's body

Can someone assist with my drop-down menu issue for the link "services"? The link disappears behind the page content when I try to modify it. Any help would be appreciated, thank you! This is the CSS: .bodycontent { margin: 0 25% 0 25%; ...

Issues with Flexbox not being rendered correctly on Safari 5.1 for Windows

My goal is to design a straightforward box for questions, with the question number on one side and the text on the other. To ensure that the texts are aligned vertically and dynamic, I am experimenting with using flexbox. I have researched both the old an ...