{"id":693,"date":"2015-10-31T00:13:49","date_gmt":"2015-10-30T23:13:49","guid":{"rendered":"http:\/\/getgui.com\/blog\/?p=693"},"modified":"2020-08-24T09:45:57","modified_gmt":"2020-08-24T07:45:57","slug":"firefox-not-scrolling-to-anchor-links-tags-position-solution","status":"publish","type":"post","link":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/","title":{"rendered":"Firefox not scrolling to Anchor Links Tags position (Solution)"},"content":{"rendered":"<p style=\"text-align: right; font-size: 12px;\"><a href=\"#solution\">Go straight to the solution<\/a><\/p>\n<h5>Issue<\/h5>\n<p>When clicking on an internal link, Firefox doesn&#8217;t scroll to the position of the anchored \/ hashtag \/ # element, during the first page load. Instead, it jumps to the middle or to the bottom of the page. If user clicks inside of the address bar and presses Enter, Firefox then scrolls to the correct position. Problem only occurs when link is not on the same page as the anchored element. If both are on the same page, Firefox scrolls to the correct position. Bug is not present in other browsers.<\/p>\n<p><em>Example: If you click on link &#8211; <a href=\"https:\/\/getgui.com\/blog\/pc-computer-laptop-repair-tools\/#getgui-current-measuring-adapter\" target=\"_blank\" rel=\"noopener noreferrer\">GetGui DC charging adapter with ammeter<\/a> New tab opens and Firefox should scroll directly to the element with ID &#8220;getgui-current-measuring-adapter&#8221;. Instead it scrolls to the middle or to the bottom of the page, as seen in the screenshot below.<\/em><\/p>\n<p><a href=\"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2015\/10\/firefox-anchor-link-scroll-bug.png\"><img decoding=\"async\" src=\"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2015\/10\/firefox-anchor-link-scroll-bug-300x117.png\" alt=\"firefox-anchor-link-scroll-bug\" width=\"300\" height=\"117\" class=\"aligncenter size-medium wp-image-724\" srcset=\"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2015\/10\/firefox-anchor-link-scroll-bug-300x117.png 300w, https:\/\/getgui.com\/blog\/wp-content\/uploads\/2015\/10\/firefox-anchor-link-scroll-bug.png 939w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Problem occurs because Firefox tries to scroll to the anchored element before the page is even loaded and the element is actually created. Since it can&#8217;t locate the anchored element, it scrolls to the middle or to the bottom of the page.<\/p>\n<p><!--more--><\/p>\n<h5 id=\"solution\">Solution<\/h5>\n<p>Workaround takes advantage of the Javascript&#8217;s <strong>window.onload<\/strong> event handler, <strong>location.hash<\/strong> property and <strong>scrollIntoView<\/strong> method. All three are supported in all the major browsers.<\/p>\n<p>\nFunction is simple and straightforward, as seen below.    <\/p>\n<pre lang=\"javascript\">\r\n...\r\n<script>\r\n\/*location.hash returns the anchor part of an URL as a string, \r\nwith hash (#) symbol included. *\/\r\n\/\/wait for page elements to load\r\n\/\/execute function only if the anchor exists in the URL address\r\nwindow.onload = function() {if(location.hash){\r\n    \/\/remove # from the string\r\n    var elId = location.hash.replace('#','');\r\n    \/\/locate the anchored element on the page by its ID property \r\n    var scrollToEl = document.getElementById(elId);\r\n    \/\/scroll to the anchored element\r\n    scrollToEl.scrollIntoView(true);   \r\n}\r\n }\r\n<\/script> \r\n<\/body>\r\n<\/html>\r\n<\/pre>\n<\/p>\n<p>Copy the code below and paste it inside of every page, just <strong>above the closing body tag<\/strong>, as seen in the example above.<br \/>\n<em>Note: Code can be copied just to the footer element, if the same footer is shared between the pages.<\/em><\/p>\n<pre lang=\"javascript\">\r\n<script>\r\nwindow.onload = function() {if(location.hash){\r\n    var elId = location.hash.replace('#','');\r\n    var scrollToEl = document.getElementById(elId);\r\n    scrollToEl.scrollIntoView(true);   \r\n}\r\n }\r\n<\/script>\r\n<\/pre>\n<\/p>\n<h5>Below is a modified code contributed by the user Thor<\/h5>\n<p>\nOne very slight mod to prevent a javascript error when your URL contains a hash but the element does not exist on the page (odd case, I know\u2026 but working on a site where this was important). It won\u2019t scroll, but also won\u2019t error now. Your code snippet was a life saver\u2026 so many answers out there that do not work. Thanks!<\/p>\n<pre lang=\"javascript\">\r\n<script>\r\nwindow.onload = function() {\r\nif(location.hash){\r\nvar elId = location.hash.replace(\u2018#\u2019,\u201d);\r\nvar scrollToEl = document.getElementById(elId);\r\nif(scrollToEl) scrollToEl.scrollIntoView(true);\r\n}\r\n}\r\n<\/script>\r\n<\/pre>\n<\/p>\n<hr>\n<\/hr>\n<p>If you have found this blog post useful and would like to buy me a cup of coffee, please click <a href=\"https:\/\/getgui.com\/blog\/donations\/\">here<\/a> :<span>)<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go straight to the solution Issue When clicking on an internal link, Firefox doesn&#8217;t scroll to the position of the anchored \/ hashtag \/ # element, during the first page load. Instead, it jumps to the middle or to the bottom of the page. If user clicks inside of the address bar and presses Enter, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[107,108,109,110],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>Firefox not scrolling to Anchor Links Tags position (Solution) | GetGui.com<\/title>\r\n<meta name=\"description\" content=\"When internal link is clicked, Firefox doesn&#039;t scroll to the position of the anchored \/ tag \/ # element, instead, it jumps to the bottom of the page.\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Firefox not scrolling to Anchor Links Tags position (Solution) | GetGui.com\" \/>\r\n<meta property=\"og:description\" content=\"When internal link is clicked, Firefox doesn&#039;t scroll to the position of the anchored \/ tag \/ # element, instead, it jumps to the bottom of the page.\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\" \/>\r\n<meta property=\"og:site_name\" content=\"Get Guides on PC Laptop Computer Repair | GetGui.com\" \/>\r\n<meta property=\"article:published_time\" content=\"2015-10-30T23:13:49+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2020-08-24T07:45:57+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2015\/10\/firefox-anchor-link-scroll-bug-300x117.png\" \/>\r\n<meta name=\"author\" content=\"Simon\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Simon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\"},\"author\":{\"name\":\"Simon\",\"@id\":\"https:\/\/getgui.com\/blog\/#\/schema\/person\/1d585f45816607e4939364f30d6147ca\"},\"headline\":\"Firefox not scrolling to Anchor Links Tags position (Solution)\",\"datePublished\":\"2015-10-30T23:13:49+00:00\",\"dateModified\":\"2020-08-24T07:45:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\"},\"wordCount\":365,\"commentCount\":15,\"publisher\":{\"@id\":\"https:\/\/getgui.com\/blog\/#organization\"},\"keywords\":[\"Firefox anchor tags not working\",\"Firefox anchored links not working\",\"Firefox doesn't scroll to anchored\",\"Firefox doesn't scroll to element\"],\"articleSection\":[\"Web development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\",\"url\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\",\"name\":\"Firefox not scrolling to Anchor Links Tags position (Solution) | GetGui.com\",\"isPartOf\":{\"@id\":\"https:\/\/getgui.com\/blog\/#website\"},\"datePublished\":\"2015-10-30T23:13:49+00:00\",\"dateModified\":\"2020-08-24T07:45:57+00:00\",\"description\":\"When internal link is clicked, Firefox doesn't scroll to the position of the anchored \/ tag \/ # element, instead, it jumps to the bottom of the page.\",\"breadcrumb\":{\"@id\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/getgui.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Firefox not scrolling to Anchor Links Tags position (Solution)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/getgui.com\/blog\/#website\",\"url\":\"https:\/\/getgui.com\/blog\/\",\"name\":\"Get Guides on PC Laptop Computer Repair | GetGui.com\",\"description\":\"Simple and easy to understand guides on PC Laptop Computer Repair. Repair your own devices. Save money. Save the planet. Save the day.\",\"publisher\":{\"@id\":\"https:\/\/getgui.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/getgui.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/getgui.com\/blog\/#organization\",\"name\":\"GetGUI\",\"url\":\"https:\/\/getgui.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/getgui.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2020\/06\/logo-instagram-large.png\",\"contentUrl\":\"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2020\/06\/logo-instagram-large.png\",\"width\":1440,\"height\":1440,\"caption\":\"GetGUI\"},\"image\":{\"@id\":\"https:\/\/getgui.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/getgui.com\/blog\/#\/schema\/person\/1d585f45816607e4939364f30d6147ca\",\"name\":\"Simon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/getgui.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7e8b868d6eaafc8da11844ba981f76cb?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7e8b868d6eaafc8da11844ba981f76cb?s=96&d=mm&r=g\",\"caption\":\"Simon\"}}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Firefox not scrolling to Anchor Links Tags position (Solution) | GetGui.com","description":"When internal link is clicked, Firefox doesn't scroll to the position of the anchored \/ tag \/ # element, instead, it jumps to the bottom of the page.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/","og_locale":"en_US","og_type":"article","og_title":"Firefox not scrolling to Anchor Links Tags position (Solution) | GetGui.com","og_description":"When internal link is clicked, Firefox doesn't scroll to the position of the anchored \/ tag \/ # element, instead, it jumps to the bottom of the page.","og_url":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/","og_site_name":"Get Guides on PC Laptop Computer Repair | GetGui.com","article_published_time":"2015-10-30T23:13:49+00:00","article_modified_time":"2020-08-24T07:45:57+00:00","og_image":[{"url":"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2015\/10\/firefox-anchor-link-scroll-bug-300x117.png"}],"author":"Simon","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Simon","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#article","isPartOf":{"@id":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/"},"author":{"name":"Simon","@id":"https:\/\/getgui.com\/blog\/#\/schema\/person\/1d585f45816607e4939364f30d6147ca"},"headline":"Firefox not scrolling to Anchor Links Tags position (Solution)","datePublished":"2015-10-30T23:13:49+00:00","dateModified":"2020-08-24T07:45:57+00:00","mainEntityOfPage":{"@id":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/"},"wordCount":365,"commentCount":15,"publisher":{"@id":"https:\/\/getgui.com\/blog\/#organization"},"keywords":["Firefox anchor tags not working","Firefox anchored links not working","Firefox doesn't scroll to anchored","Firefox doesn't scroll to element"],"articleSection":["Web development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/","url":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/","name":"Firefox not scrolling to Anchor Links Tags position (Solution) | GetGui.com","isPartOf":{"@id":"https:\/\/getgui.com\/blog\/#website"},"datePublished":"2015-10-30T23:13:49+00:00","dateModified":"2020-08-24T07:45:57+00:00","description":"When internal link is clicked, Firefox doesn't scroll to the position of the anchored \/ tag \/ # element, instead, it jumps to the bottom of the page.","breadcrumb":{"@id":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/getgui.com\/blog\/web-development\/firefox-not-scrolling-to-anchor-links-tags-position-solution\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/getgui.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Firefox not scrolling to Anchor Links Tags position (Solution)"}]},{"@type":"WebSite","@id":"https:\/\/getgui.com\/blog\/#website","url":"https:\/\/getgui.com\/blog\/","name":"Get Guides on PC Laptop Computer Repair | GetGui.com","description":"Simple and easy to understand guides on PC Laptop Computer Repair. Repair your own devices. Save money. Save the planet. Save the day.","publisher":{"@id":"https:\/\/getgui.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/getgui.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/getgui.com\/blog\/#organization","name":"GetGUI","url":"https:\/\/getgui.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/getgui.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2020\/06\/logo-instagram-large.png","contentUrl":"https:\/\/getgui.com\/blog\/wp-content\/uploads\/2020\/06\/logo-instagram-large.png","width":1440,"height":1440,"caption":"GetGUI"},"image":{"@id":"https:\/\/getgui.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/getgui.com\/blog\/#\/schema\/person\/1d585f45816607e4939364f30d6147ca","name":"Simon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/getgui.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7e8b868d6eaafc8da11844ba981f76cb?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7e8b868d6eaafc8da11844ba981f76cb?s=96&d=mm&r=g","caption":"Simon"}}]}},"_links":{"self":[{"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/posts\/693"}],"collection":[{"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":76,"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":32304,"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/posts\/693\/revisions\/32304"}],"wp:attachment":[{"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/getgui.com\/blog\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}