Implement ScrollTo() on the page that has just been opened

I am exploring a method to implement the scrollTo() function on a new page after a specific link has been clicked. Unfortunately, utilizing window.onload is not suitable for my scenario as I only want the scrolling action to occur when the page is accessed through a particular link.

My goal is to have the home page redirect and smoothly scroll to the 'other' section of the services page:

HTML:

<a onclick="otherServices">Other Services</a>

JavaScript:

function otherServices() {
    window.location = "/services";
    win = window.location;
    setTimeout(win.scrollTo(0,document.body.scrollHeight), 3000);
}

Ideally, I would like to steer clear of using jQuery if possible.

Answer №1

you can navigate to a specific section on the website by using hash.

website navigation

<a href="/products#top">Top Products</a>

product page script

window.onload = function () {
  if (window.location.hash == '#top') {
    window.scrollTo(0,document.body.scrollHeight);
  }
}

Alternatively, you can achieve the same effect without JavaScript by setting the hash in the link to the Id of the desired element.

Answer №2

To achieve the desired functionality, you can implement a script that executes on page load and checks if the current page is the services page. If it is, then it scrolls to a specific section on the page (referred to as "somewhere").

For the homepage:

<a href="/services">Other Services</a>

For the services page:

window.onload = function () {
  if (window.location.pathname === '/services') {
    window.scrollTo(0, somewhere)
  }
}

Update: Upon reevaluation of the issue, a better solution has been found:

For the homepage:

<a href="/services#section-id">Other Services</a>

For the services page:

<section id="section-id">Other Services</section>

The browser will handle the scrolling automatically based on the anchor tag or section ID reference provided.

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

Jade: modify the text box input to show = " ..."

I am new to Jade and Html, and I am currently working on creating a form in Jade. The issue I am facing involves a simple text box that looks like this: input(type='text', name='sessionID', value = 'valueID') Whenever I want ...

The basic Node.js API for greeting the world encountered a connection failure

I'm currently working on setting up a basic hello world route using nodejs and express. After running node index.js, I see listening on port 3000 in the console, but when I attempt to access http://localhost:3000/helloworld, it just keeps trying to co ...

JavaScript and the importance of using commas in arrays

I am developing a system that displays text in a textarea when a checkbox is checked and removes the text when the checkbox is unchecked. The functionality is mostly working as intended, but I am facing an issue where commas remain in the textarea after un ...

In order for jQuery autocomplete to function properly, the corresponding <input> tag must be utilized at least once after launching the browser

I have recently integrated jQuery into my code and decided to utilize the latest jQuery autocomplete plugin for a project I am working on. The feature essentially allows users to log in by typing their username, with the autocomplete functionality retriev ...

Tips for seamlessly incorporating React Epub Reader into your next js project

Having some trouble integrating the react epub reader with Next Js. It's working perfectly fine with react js, but I keep encountering an error. Can you help me figure out how to integrate the epub reader with next js? Here is the error This is my ...

Eliminate the hovering effect from images that are hyperlinked

I am feeling incredibly desperate as I have spent hours searching the internet for a solution with no success. When it comes to text links, I have included the following CSS code: a:hover img { border-bottom: none !important; } However, this code is als ...

What is the best method for specifying CSS styles for a Vue.js component during the registration process?

Is it possible to register a custom Vue.js component using the following code? // register Vue.component('my-component', { template: '<div class="my-class">A custom component!</div>' }) For more information, you ...

Form an object using elements of a string array

Trying to convert a string array into an object. The string array is as follows : let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world']; I want the resulting obje ...

Using jQuery to append content does not automatically attach an AJAX event listener to the newly appended element

Here is a script I have for appending new elements: function(addNewElements, information, link){ var $newAdditions = $( addNewElements ); $('#posts').masonry( 'appended', $newAdditions, true); } This button is included in th ...

Is it possible to access all the variables within a specific scope or explore different scopes using console.log?

In the process of writing a graph algorithm, I am currently working on implementing a removeEdge method in the graph prototype. This method takes two arguments, which are the two nodes that will be losing their connection. I have successfully achieved a ...

Tips for structuring commands in Discord.js

I'm in the process of restructuring my bot's commands. I currently have a folder called commands with all my commands inside, but I want to make it more organized by categorizing them into moderators, fun, and global like this: commands > mo ...

Ext JS - A grid cell containing varying values, accompanied by a selection of combo boxes

I'm currently implementing the Ext JS Grid component and have a list of fields with their respective data types: ID (int) Name (string) Foods (List<string>) Each user can have multiple foods, selected from a Food DataStore. Displaying this in ...

Lens Flare Troubles in Three JS

I'm having trouble implementing the LensFlare Effect from the three js docs in react three fiber. I've tried using the code provided but it's not working for me. Here's what I have: import { extend, useLoader } from "@react-three/f ...

Prevent duplicate rectangles by cleaning up dirty rectangles

I'm looking to optimize the performance of my game by only clearing the necessary parts of the canvas that contain animations. Below is a snippet of my code: this.draw = function(context) { context.clearRect(this.oldx, this.oldy, this.width, ...

Ways to expand Sequelize model

Can a model be extended or inherited in order to add hooks and fields after the model has been defined? Is there a method for achieving this, similar to the following example: User = sequelize.define("user", { name: sequelize.String }); makeStateful( ...

Steps to develop a countdown timer for every iteration of sending an API request using a specified list

Recently, I encountered a challenge with my JavaScript array that holds 40 items. My goal was to develop a function capable of cycling through each item in the array and initiating an API call based on the current value. The caveat here is that I needed a ...

Displaying a clock using PHP

Seeking a way to display server time on my website, I have successfully implemented client-side time using JavaScript. How can I now showcase the server time? Are there any ZendFramework classes that can assist with this? Currently, I am utilizing the fo ...

How can I dynamically show a div in VueJS depending on the selected option?

I am working on a VueJS project where I need to create a dynamic dropdown list that displays different divs based on the selected option. Below is the code snippet: export default { data(){ return{ selected: "Choose Option", opt ...

Vue.js: EventBus.$on is not properly transmitting the received value

I recently started working with Vue and am currently exploring the best way to organize my event bus. In my project, I have a main layout view (Main.vue) that includes a router view where I pass emitted information from a child component like this: <te ...

Error: The function could not be located

My HTML page is quite straightforward: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name=&quo ...