Link to one-page from sub-page

5 replies · opened Jan 23, 2024

FFred_SentryJan 23, 2024

Hello,
I'm using a One-Page site (Canvas 7.2.2), with a single menu to scroll to the sections of the page. See below:


    
        
- [Home](#slider)
        
- [Overview](#section-overview)
        
- [Features](#section-features)
        
- [Blog](#section-blog)
    

Works like a charm.
But I also have additional subpages (for each blog post), and I want the user to be able to return to the main page directly to a section.
I use the menu below (links to homepage + hash to the right section), with some javascript to get the hash from the URL and link to the right section.


    
        
- [Home](/#slider)
        
- [Overview](/#section-overview)
        
- [Features](/#section-features)
        
- [Blog](/#section-blog)
    

Here is my problem: less than 1 sec after returning to the homepage and the right section, the page scroll up to the top then scroll down again.
How to avoid the unwanted scroll up effect? What is the native function in the template that produces this automatic scroll up?
Thanks for your help.
Regards.

FFred_SentryJan 23, 2024

And here is the "some javascript" I'm refering to in my first message:

// **************************************************************
// This function aims to get the hash in the URL when the user 
// comes from a blog and clicks to a section of the homepage
// **************************************************************
function scrollToHash() {
	// Get the hash from the URL
	var hashToScroll = window.location.hash;

	// Check if a hash exists
	if (hashToScroll) {
		// Remove the '#' symbol from the hash
		var targetId = hashToScroll.substring(1);
		// Find the target element by ID
		var targetElement = document.getElementById(targetId);
		// Check if the target element exists
		if (targetElement) {
			// Scroll to the target element without smooth scroll
			targetElement.scrollIntoView({ behavior: 'auto', block: 'start', inline: 'nearest' });
			//targetElement.scrollIntoView();
			//document.getElementById(targetId).scrollIntoView();
			// Clear the hash from the URL
			history.replaceState(null, null, ' ');
		}
	}
	}
// Call the function when the page has finished loading
window.addEventListener('load', scrollToHash);
FFred_SentryJan 30, 2024

No matter, we finally manage to solve this problem ourselves, by working around the problem.
Thanks.
Regards.

SSemicolon WebSTAFFFeb 1, 2024

Hello,

Thanks for your Kind Patience and Apologies for the Delays in replying!

Including this JS code should fix this issue:
[ch_pre type="js"]window.onbeforeunload = function() {
SEMICOLON.Core.scrollTo(0, 0, false, 'auto');
};

SEMICOLON.Core.scrollTo(0, 0, false, 'auto');[/ch_pre]

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

FFred_SentryFeb 7, 2024

Hello,

Thank you for your answer.
Can you explain where exactly do I need to include the JS code you provided?

Thanks.
Regards.

SSemicolon WebSTAFFFeb 9, 2024

Hello,

Make sure that you are adding this code inside the HTML Page where the One-Page Section is.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard