www.getmdl.io is impossible to use due to hashtag link bug
- Click "GETTING STARTED"
- Click one of the "Contents" links, such as "General rules and principles"
- Scroll up and wait a few seconds, scroll around the page and wait a few more seconds. The page will keep jumping back to "General rules and principles"
- Click any of the other content links, and it keeps the same "#general-rules" hash in the URL, and incorrectly jumps to that section again.
I shared material design lite with a friend that didn't want to use bootstrap, and they were immediately turned off but the whacky page behavior.
(Confirmed on OSX 10.10.5 and Chrome 46.0.2490.80)
MDL doesn't have affix or something simular, so the hashtag doesn't update
Can you try downloading Chrome Canary and testing with that? It is most likely a bug in Chrome stable and we are just waiting for the fix to get pushed up. Sadly, nothing we can really do about it.
@Garbee just tested it with a new profile in canary. the hastag in the url isn't updated.
I just tested in Canary as well and am not seeing the problem. Stable is exhibiting issues, which tells me this is the same Chrome bug that just isn't fixed yet. Nothing we can do about it here except wait for a new stable (or possibly two if beta doesn't have the patch.)
@Garbee we could file a crbug right?
@samccone No point. It is already fixed, just need to wait for next stable. Plus, I think someone already has filed one or two reports about this issue from MDL. Last time the problem came up it was mentioned.
There isn't a temporary work around? If not, I think it would be worth putting a note that it doesn't work properly in Chrome and that Chrome is at fault. It is a huge turn off to new users because they will probably assume it is a MDL library bug and will not want to use the library.
Deploying a site update isn't exactly trivial. A new version of Chrome will be out in about 4 weeks.
I'm not sure a note will help much. If there is a note, people will just assume we are too lazy to find a work-around. It actually is no custom code at fault here, since we are just doing hash links which all browsers support to id'd elements. Either way, it looks bad.
Is this related to where hash links don't work on an MDL website? https://nehta-au.github.io/simple-patient.html#Step2 for example does not take you to the link "Step2", but loads to the top of the page.
This is definitely an MDL issue and not isolated to Chrome browser (desktop/mobile). I see the exact same issue with product#notification on Chrome, Firefox, Safari, and Opera
Solutions such as this one and the one below, also have no affect.
$(function () { 'use strict';
if (window.location.hash) {
var top = $(window.location.hash).offset().top;
$(window).scrollTop(top);
}
});
This issue should not be closed.
@minessis Different issue actually.
The issue here was a Chrome specific bug where due to the way we implemented hash jumping internally when you scrolled it caused the jump to happen again. Therefore sticking a user into that hash for that load.
Your issue is another known issue with the way we implemented the layout to get IE9 support. Sadly, that can't be addressed internally due to BC concerns.
Not sure what is a BC concern, but is that a confirmation that any website made with MDL cannot support linking to an anchor inside a webpage? Sorry, I'm new to webdev.
@Garbee thank you for the reply, is there a workaround for (the way we implemented the layout to get IE9 support), i.e. for a working solution to linkto www.example.com/foo#bar ?
@vadimnehta Backwards Compatability
You have to manually scroll the content area post-load like the components page setup.
@Garbee are you referring to line 134 from components page setup?
if (section !== '#' + linkWithoutHash) {
history.pushState(null, 'Material Design Lite', link);
// Scroll to top of page
document.getElementById('content').scrollTop = 0;
// Track the specific component page view in Google analytics
if (ga) {
ga('send', 'pageview', '/components/' + linkWithoutHash);
}
}
If so, how is this different than:
$(function () { 'use strict';
if (window.location.hash) {
var top = $(window.location.hash).offset().top;
$(window).scrollTop(top);
}
});
Which does not work.
You're scrolling the window instead of the content area.
For support please use StackOverflow so others can easily search and gain knowledge from the answers. Your custom code isn't related to what MDL provides. Therefore, the MDL issue tracker isn't the proper place for that kind of discussion.
@Garbee @minessis here is how i am solving it,
issue seems because mdl-js-layout (MaterialLayout) component detach the content area and then after wrapping it into another container
/**
* Initialize element.
*/
MaterialLayout.prototype.init = function() {
if (this.element_) {
var container = document.createElement('div');
container.classList.add(this.CssClasses_.CONTAINER);
var focusedElement = this.element_.querySelector(':focus');
this.element_.parentElement.insertBefore(container, this.element_);
this.element_.parentElement.removeChild(this.element_);
container.appendChild(this.element_);
//....
}
so i am fixing it as bellow
var hash = window.location.hash.replace("#", "");
if (hash) {
var $mdlLayout = $('.mdl-js-layout');
$mdlLayout.on("mdl-componentupgraded.mdl-hash-issue", function(){
console.log("componentupgraded")
$mdlLayout.off("mdl-componentupgraded.mdl-hash-issue");
setTimeout(function(){
scrollTo("#"+hash)
}, 1)
})
};
var $scroller = $('.mdl-layout__content');
function scrollTo(selector, event){
var $to = $(selector);
if (!$to.length)
return
event && event.preventDefault();
if($to.offset().top == $scroller.offset().top)
return
//$scroller.data("is-animating", true);
$scroller.animate({
scrollTop: $scroller.scrollTop() + $to.offset().top - $scroller.offset().top// + 10000
},{
done: function(){
//$scroller.data("is-animating", false);
//setTimeout(function(){$scroller.trigger("scroll")}, 100);
//selector && selector[0] == "#" && (window.location.hash = selector);
}
});
}