Attempting to make this Wordpress theme smoothly navigate to a specific id when a link is clicked from another page

If you take a look at the website haloespresso.com.au/working/, you'll see that when you click on the "menu" option in the top menu, it scrolls to the menu id #pg-9-4. This is exactly what I am aiming for. However, on other pages, the menu is slightly different and the same link redirects to the home page with #pg-9-4 added to the end of it. The idea here is to have the link from another page open the home page but scroll directly to the menu section. Despite my efforts, I cannot seem to figure out why it keeps jumping back to the top instead of staying at the specified spot. It briefly goes there for a split second as the page loads, but then immediately returns to the top. I am struggling to understand what is causing this issue and hindering the basic HTML functionality that should keep me anchored to the designated menu area.

I would greatly appreciate any assistance with resolving this matter, as my expertise does not extend much beyond html/css and simple jquery.

Answer №1

To make it work, you need to add the anchor at the end of the hyperlink.

Simply include a link in this format:

<a href="/anotherpage.html#section6">Link to specific section on another page</a>

Update: It seems like you're having trouble implementing this. Can you share how your links are structured and what the relevant HTML looks like on the target page?

Answer №2

Here is a useful jQuery code snippet to implement:

$(document).ready(function() {
   function scrollToHash() {
      // Retrieve URL Hash
      var hash = window.location.hash;

      // Check if hash exists and is not empty
      if (hash != '') {
         // Scroll to element with matching ID after a slight delay
         setTimeout(function() {
            $(document).scrollTop($(hash).offset().top);
         }, 10);    
         // Debugging for reference
         console.log(hash);
         console.log('offset:' + $(hash).offset().top );
     }  
   }
   scrollToHash(); // Trigger the scroll function based on hash
});

Explanation:

This code captures the URL hash (e.g., www.example.com/#hash), verifies its presence and scrolls the page to the element with the matching ID after a brief delay of 10 milliseconds. This delay helps prevent browser loading issues.

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

Starting from index 1, I am crafting a JSON array

I am trying to generate a JSON array that starts with index 1 Below is the code I am using to extract images from websites: $image_urls = array(); //retrieve all image URLs from the content foreach($get_content->find(&ap ...

The commands 'npm run watch' and 'dev' are no longer functioning on my computer for my Laravel project. I am encountering an error stating: '[webpack-cli] RangeError: Maximum call stack size exceeded'

Recently, while working on a Laravel website, I've been focusing on writing JavaScript code to integrate Meilisearch. To facilitate this process, I incorporated the 'dotenv' library with node to access variables from my .env file for securit ...

Unable to make a commit with husky: encountering an issue with the .husky/pre-commit script, specifically on line 4 where the "npx"

For several months, I had husky installed and everything was working perfectly. However, today out of nowhere, after restarting my system, I encountered this unexpected error: Error: husky - Command not found in PATH=/Applications/Xcode.app/Contents/Develo ...

What is the best way to conceal the <option> tag if the value is empty?

<select name="" id="pftb"> <option value="<?php echo $pf1;?>"><?php echo $pf1;?></option> <option value="<?php echo is_null($pf2) ? "" : $pf2; ?>" ><?php echo ...

Receiving an error of "undefined" when trying to capture the selected

One issue I am facing is capturing the selected user option and sending that value in a post request. Let's put aside the post part since it's not directly related to the main question at hand. Currently, the value is showing up as undefined. ...

Efficient - Managing numerous database connections

I am currently working on a project using node.js and I have created a login form where users need to select the database they want to connect to. However, I am uncertain about how to establish a connection with the selected database. Is there a way for m ...

Removing an object in Three.js using scene.getObjectByName() method

When developing my game, I encountered a challenge. I'm loading 4 different buildings and creating 15 clones of each within a for loop. These clones are then added to an array called masterBuildingArray, which I iterate through using a forEach loop. T ...

How can I show a form and table on the same URL using Django?

Currently, I am delving into learning Django programming. I am in the process of creating a basic application that uses a form to update a referenced table. Everything was working fine until I decided to assign two different URLs for the table and forms. ...

Looking to transform a JSON Object into a JSON Array for compatibility with Highcharts

I'm feeling really stuck on this one. Highcharts requires a specific data format for the series, like so: [ { name: 'Title Here', data: [1,2,3,4,5] } ] The problem I'm facing is that when my PHP ajax uses json_encode(), it converts th ...

Replace the first-child element in a CSS class with a different class

Within a webpage, there is a table nested inside another table. The CSS class first-child assigns a blue background to the first row. How can I prevent this specific styling from affecting the nested table? Please keep in mind that modifying the original ...

Can Javascript have IDs within hrefs?

Is it possible to insert an href with an id inside the runConfirm function? My goal is to display a pop-up after clicking accept, where the accept button triggers the query from isset. Below is the code for accepting: $displaybody .= "<tr> <td&g ...

Database connection error: Issue with deploying Google App Engine

I am currently using a 2nd generation instance and facing connectivity issues. Despite being able to connect locally, I encounter an Error establishing a database connection when running gcloud app deploy. I have attempted to use both 'root' with ...

What is the best way to create a clickable anchor tag that is nested within a paragraph tag?

Here is the string I am working with: String txtLink = "<p>Apply directly in our <a href=\"https://theorem.applytojob.com/apply/TyJy6YCsOs/Experienced- Backend-Engineer-Ruby?source=GitHub+Jobs\">careers page</a></p>"; ...

What could be causing the type error to show up in my JavaScript file?

Why is this error occurring? I am currently studying web development through Colt Steele's web bootcamp and encountered this issue. $ node user.js mongo connection is open D:\web development practice\mongoose_relationship\Models\u ...

Maintain the text layout when copying and pasting from the Angular Application

After noticing that copying and pasting text from an Angular Application to a text editor like Microsoft Word results in losing the original format, I decided to investigate further. An example I used was the angular material website: https://material.ang ...

Can the getState() method be utilized within a reducer function?

I have encountered an issue with my reducers. The login reducer is functioning properly, but when I added a logout reducer, it stopped working. export const rootReducer = combineReducers({ login: loginReducer, logout: logoutReducer }); export c ...

Exploring the Power of Webpack and Web Workers within Ionic 2

In our upcoming Ionic 2 project, we are considering the implementation of web workers. However, due to the usage of ionic-app-scripts (version 1.0.0) with webpack in Ionic 2 (https://github.com/ionic-team/ionic-app-scripts), we face the challenge of having ...

The script fails to function properly when it is accessed from a different webpage

I have been working on a website using JQuery and JQuery Mobile. As I test the pages one by one, I created a page where users can input data and get autocomplete suggestions from a PHP/MySQL database. The functionality works perfectly as intended. However ...

What could be the reason for my function suddenly needing the password to be filled in?

Code snippet in index.js: // To handle sign-in form requests, I will be using bcrypt app.post('/auth', async (req, res) => { // Check if the userName exists in the database and retrieve the hashed password for that account let password = ...

What is the inner workings of CSS?

I'm curious about the inner workings of CSS. Does the HTML get interpreted before CSS, or vice versa? Or does it all apply as soon as the browser has constructed the DOM? I would appreciate a detailed explanation on this topic. ...