Hi Guys,
We have links like href="#" ==> they can be avoided by javascript:void(0)
But we have links where the link is supposed to redirect on another page and scroll to a certain div id like href = "/path/to/page#div-id"
In this case instead of redirecting to the page it either scrolls down if it can find the #div-id or throws an error.
Uncaught TypeError: Cannot read property 'top' of null
element.scrollTo
bootstrap.js it occurs at line 134
this function ==>
bootstrapAnchor: function (element) {
element.validAnchor = element.nodeName === 'A' && (location.hostname === element.hostname || !element.hostname) && element.hash.replace(/#/,'').length;
element.scrollTo = function(event) {
var attr = 'id';
var $target = $(element.hash);
if (!$target.length) {
attr = 'name';
$target = $('[name="' + element.hash.replace('#', '') + '"');
}
var offset = $target.offset().top - parseInt($scrollableElement.css('paddingTop'), 10) - parseInt($scrollableElement.css('marginTop'), 10);
Perhaps We are missing out something,
just some help would be much appreciated ,
thanks :)
| Comment | File | Size | Author |
|---|
Comments
Comment #1
mathieso commentedHey,
bootstrap.js seems to think that every <a> might be an anchor. Lines 113 or there abouts:
For one page with admin menu on it, this processed 591 links, adding scrolly stuff to each one (I think anyway).
Changed code to this:
The code only process links that have # as the href. It seems to work.
However, I'm no JS guru. No doubt some other human could do better.
Kieran
Comment #2
aukhan commentedHey thanks for the update and sorry for the rather late response from my side,
i read your code and i think its great,
Was just reluctant to make changes ... have not yet made my first contribution :(
Id confirm that adding the checks mentioned in the code above are working for me,
great work,
Thanks
Comment #3
attiks commentedI ran into something similar, I fixed it as follows
Comment #4
wangjiaqi commentedit seems that you miss the #1, my site couldn't work without #1
Comment #5
markhalliwellThanks for working on this @attiks. Unfortunately it's a little more complicated than just returning early. We need to also check to see if there's any elements that use the name attributes instead of it's ID (think form elements :)).
I've also gone ahead and fixed another issue while I was in there. The $fakeElement was trying to attach itself to the document, it should be prepended to BODY instead.
Committed 46ecffc to 7.x-3.x:
Comment #6
acbramley commentedI was getting these errors with the close buttons on the drupal messages, fixed it by disabling the Anchor settings in the theme settings under Javascript > Anchors.
Comment #7
markhalliwellComment #10
markhalliwellI'm just moving this back to 7.x. If this needs re-evaluation in 8.x, create a new issue.
Comment #11
brtamas commentedFor me the attached patch worked.
Comment #12
brian.sanchez commentedyes this worked for me, thank you!
Comment #13
heacu commentedthis patch worked for me, although the anchors are slightly cut off at the top of my window, as opposed to them being perfect when i disable the "Fix anchor" setting. for this reason i applied the patch but am not actually using it since the result is better without it.
Comment #14
Denis Danielyan commented#11 works like a charm, it would be great if this could be merged back into 7.x-3.0 as the functionality is definitely broken
Comment #15
pawel_r commented#11 solved problems
Comment #16
markhalliwellFWIW, this anchor crap (which I can say, cause I originally created it) is finally getting replaced with a proper plugin :)
Comment #17
sobi3ch commented#11 works for me
Comment #18
markhalliwell#11 isn't going to get committed
Comment #19
praveen90 commentedWow... dudes.. Working Perfect. Apply both patches :D
Comment #20
bryden commented#6 worked for me
Comment #21
ynakano.endertech commentedI needed #1 and #6.
I applied #6 first, then the standard HTML anchor "name" attribute started to work. Before that, this standard HTML code did not work:
However, #6 fix broke the control arrows on Bootstrap carousel. The arrows stopped advancing the carousel slideshow.
I had to apply #1 to fix the problem.
In the end, #1 and #6 were enough to solve my problem.
Comment #22
joshmillerThe patch in #11 didn't apply for me, but applying it manually solved my JS error.
Comment #23
ConradFlashback commented#6 with #11 works for me.
Thanks.
Comment #24
stefan lehmannWe also had some trouble with the implementation of that particular javascript file in Bootstrap 3.0. It also affected our Bootstrap carousels and our "to top" links, as these also work with anchor links.
Here is a patch which also considers that.
Comment #25
markhalliwellI'm closing comments on this issue. Nothing else is going to be committed on this issue per #16.