<div id="anchorname"> should always provide an anchor point at the top of the div that you can go to using <a href="#anchorname"> (or equivalent) unless the div is located inside a table or is too low down on the page to ever get to the top of the viewport…
Some browsers will always position to the top of the table for any anchor points that are inside the table so that the column headings are visible.
This is how HTML works even without JavaScript. Setting the values from within JavaScript doesn’t change how it works.