Using a hashtag in the URL to open an HTML <div> element

I have a website with two tabs labeled as Home and Action.

When I hover over the Action tab, the URL changes to include #tab_action:

https://i.sstatic.net/OOD5S.png

Then, when I click on it, the related tab content opens:

https://i.sstatic.net/JdcGG.png

If I manually enter the URL:

localhost:8088/hse/public/observation/3/show#tab_action

It opens the Home tab instead of the Action tab.

Is there a way to make the above URL open the Action tab, just like when I click on the Action tab?

Answer №1

When the page has finished loading or when the document is ready, check the hash value. If it exists, utilize it.

$(function(){
  var hash = window.location.hash;
  if (hash && hash.length>1) {
    $(hash).click()
  }
})

Answer №2

When the page loads, make sure to verify the window.location.hash (the URL hash) and use JavaScript code to switch between tabs accordingly.

Answer №3

If you're searching for information on WindowEventHandlers.onhashchange, you can check out the details here.

if ("onhashchange" in window) {
    alert("The hashchange event is supported by this browser!");
}

function checkHash() {
    if (location.hash === "#tab_action") {
        activateTabFeature();
    }
}

window.onhashchange = checkHash;

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

Toggle the visibility of items based on the user's login status

Below is the code snippet for when the user is logged out: <div class="fusion-secondary-main-menu"> <div class="fusion-row"> <?php avada_main_menu(); ?> <?php avada_mobile_menu_search( ...

Customized Quick Access MUI

Hey there! I'm currently working with the Speed ​​dial component for MUI and I'm having trouble figuring out how to style the tooltipTytle. I can only seem to style them when they are all displayed at once, but that's not what I want. I ...

Is it possible to create a jQuery context menu item based on the value of a PHP session

I am working with a jQuery context-menu that needs to display specific menu items based on the PHP $_SESSION variable. For example, regular users should only see "new" and "edit" options, while admin users should also have the option for "Delete user". He ...

Error: An anomaly token was encountered while deploying a Laravel/Vue project using the pipeline yml in Yarn Run

I have encountered a challenge while trying to deploy my project to a server using bitbucket-pipeline with a .yml script. The project consists of a Laravel backend with PHP 7.4 and a Vue Js frontend. The issue arises during the frontend build process with ...

The HTML page is failing to call the function in the JavaScript application

I recently started taking a YouTube Javascript course on chapter 34 titled "Make start button work" Javascript tutorial My HTML, CSS, and Javascript files are all located in the same folder. I am using VS Code along with the Live Server extension for codi ...

SVGs are not resizing correctly in Internet Explorer and there is unnecessary blank space around them

Recently, I made the decision to switch to using SVG symbols for one of my projects. However, I encountered a challenge as I needed these SVGs to be responsive. My main objective was to minimize the number of HTTP requests, leading me to explore the option ...

Using more than one button to activate a single Material-UI Popper component

I recently found myself in a situation where I needed to activate the Material-UI <Popper /> component from various clickable elements. According to the Popper component API on the official Material-UI website, setting the anchorEl property determine ...

The ASP.NET fileupload control is designed to seamlessly load files without the need for

When working with ASP.NET, I am utilizing the fileupload control to transfer files to my server. However, my screen features two buttons - one for using the fileupload functionality which triggers the "File Selection Screen", and another button to submit t ...

Guide on how to refresh the background image using the identical URL within a directive

I am looking to refresh the background image of a div using the same URL within a directive. Upon loading my view, I utilized this directive to display the image as a background for the div. app.directive('backImg', function () { var dir ...

Activate a JQuery animation to change numbers when I scroll over a div for the first time

I've created a one-page-style website with some statistics in the middle. I want these numbers to count up only once when a user first sees them after refreshing the page, without using a plugin. So, I decided to implement this functionality using jQu ...

sending data from a servlet to ajax

Implementing a star-based voting system involves sending an ajax request to update the database through a servlet when a user casts their vote. This is the ajax code used: $.ajax({ type:'GET', contentType: "charset=utf- ...

What could be preventing my XPath from selecting a link/button using its label text?

<a href="javascript:void(0)" title="home"> <span class="menu_icon">Possibly more content could go here</span> Home </a> When I use //a as the XPath in the above code, it is highlighted. However, when I try //a[contains(text ...

Updating multiple collections in MongoDBRestructuring data across multiple

Imagine a scenario where an API call must update two different collections. It's crucial that if one update fails, the first update needs to be reverted. How can I guarantee that both operations either complete successfully or none at all? Let me prov ...

Cannot upload pictures using pixi.js PIXI.Texture.from()

I'm currently developing a web game using React with PixiJS. I am trying to incorporate images from my local directory into Pixi.js. For reference, I found this helpful website: Here are the snippets of code that I have used: import * as PIXI from & ...

Dealing with unreadable strings when formatting dates in a JSP

When working on a JSP project, I encountered a situation where I needed to format a Java Date retrieved from the request. My current approach looks like this: <fmt:formatDate value="${attribute.value}" pattern="yyyy-MM-dd HH:mm:ss"/> This method wo ...

Populating Dropdown list with values based on input provided in Textbox

Can you assist me in finding the solution to this issue? I have a TextBox and a DropDown list. For example, if I type "Anu" into the textbox, I want it to populate the dropdown list based on the text entered. How can I achieve this? I am working with vb. ...

How to detect and resolve the problem of an empty div tag

I'm trying to determine if a div is empty and add the text "empty" to it if it is. Can anyone help me figure out what I'm doing wrong? Thank you in advance! View my jsFiddle file HTML <div class="staff-row"> <img alt="" src="foo.j ...

The presence of an undefined variable in `esm.js` is resulting in an overwhelming amount of

After upgrading from Node v14 to Node v16, I encountered a strange error specifically when running node with node -r esm. The error message reads: ReferenceError: myVar is not defined This results in an extensive output of the esm.js module spanning 5000 ...

Dealing with textarea in Javascript

I am new to JavaScript and facing a challenge in creating a delimited string from a textarea input. The issue is that when the textarea is passed in, it includes newlines for each row. I aim to parse the entire textarea content into a string with a delimit ...

What could be the reason for the nav icon not displaying?

<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Boot ...