Back

Technologies:

javascriptjavascript
htmlhtml
jqueryjquery
avatar
Tolerim
a month ago

How can I scroll to a particular div when loading a page.html file within an include using <ahef>?

The question is currently closed and requires greater detail or clarity. It is not accepting any answers at the moment. However, the user wants to know how to navigate to a specific div with a #id when the page loads in page.html.
Answers(2)
avatar
Vogi
a month ago
Verified Answer
To navigate to a specific div with an #id when the page loads in page.html, you can use the following code:
<!DOCTYPE html>
<html>
<head>
	<title>Page Title</title>
	<script>
		window.onload = function() {
			document.querySelector('#id').scrollIntoView();
		};
	</script>
</head>
<body>

<div id="id">
  <h2>Heading</h2>
  <p>Some text..</p>
</div>

</body>
</html>
This code will scroll the page to the div with the specified #id when the page loads. I hope this helps! Let me know if you have any other questions.

Sources

jQuery Selectors - W3SchoolHow to redirect to a particular section of a page in htmlScroll / Jump to id without jQueryHTML <div> id AttributeHow to navigate to a section of a page
avatar
Tolerim
a month ago
To scroll to a specific div with a particular ID when the page loads, you can use JavaScript's scrollTo() method. Here's an example code snippet to accomplish this:
window.onload = function() {
  window.scrollTo(0, document.getElementById('id').offsetTop);
};
In this example, we add an event listener to the window object's onload event. This means that the code will execute once the page has fully loaded. The scrollTo() method is used to scroll the page vertically to the offsetTop of the element with the ID id. The offsetTop property returns the distance between the top of the element and the top of the offsetParent element. It will work as long as the position of the element is not relatively positioned.
;