Anchor links — how to catch event when using this?

Hello. I have a anchor link to the comment. The problem is that the button with href="#..." located in the comments. Why when a user clicks on such a link, the screen is lowered it too low (couldn't see the border, the border of the comment). I just want to lift the screen when using this link. I think you should use JS.
How to catch event when using anchor links? There is little one onclick - as the user from another page go to the anchor link on the page with the comments. Or simply type it in the address bar.
March 12th 20 at 08:12
2 answers
March 12th 20 at 08:14
Solution
Can you put id on the comment, or put a link and comment in the wrapper?
March 12th 20 at 08:16
Assign a variable the url of the current page with an anchor.
var url = window.location.href;
Then it is necessary to put from a url string, the substring to the anchor(option). As apart of the anchor can be any utm tags, etc. something like:
var anchor = url.search("#block320"); // if not find, return -1

If the user really clicked the link to the anchor will skryim slightly up(there are 10px, but it is better to focus on the resolution):
if (anchor != -1) { 
window.scrollBy(0,-10);
}
There are methods that return once the options have to Google. Something like window.location.href.params - cindy.OKee commented on March 12th 20 at 08:19

Find more questions by tags JavaScript