0

i have a WordPress site and problems with anchors. i have a page with several anchors which are linked to in the main menu. when i am on the page itself, all anchors work fine, but if I'am on any other page, they don't work, at least not in all browsers and the anchors are ignored.

As being informed it is a chrome bug, ive found this solution:

<script type="text/javascript">

jQuery(window).load(function(){
    var hashNum = 0;
    if (window.location.hash != ''){
        hashNum = window.location.hash.replace("#oneofmanyanchors", "");   
        console.log('hashNum: ' + hashNum); 
    };
    hashMenu = jQuery('[data-q_id="#oneofmanyanchors"]').offset().top;
      jQuery('html,body').animate({
          scrollTop: hashMenu
    }, 0);

});

 </script>

above code is working and fixes the issues i had in chrome and ff.

however i need this added functionality: At the moment it is addressing only one specific anchor, but i need it to work with any anchors in the page url, not just the one above (anchors are referenced with the data-q_id attribute).

so the code needs to be updated that it grabs any given anchor from the page URL and go to / scroll to that anchor (once) via jquery after first page load.

How do i achieve this?

Thanks in advance!

PS: The problem is caused by theme incompatibility with a certain plugin i need...

Mella
  • 35
  • 1
  • 11
  • 1
    You mean when you come to this page from another page, it doesn't scroll to the section mentioned in the 'hash'? – Mohit Bhardwaj Jun 06 '16 at 11:55
  • possible duplicate http://stackoverflow.com/questions/37606128/anchor-tag-in-chrome-not-working-properlyis-this-a-chrome-bug/37606439#37606439 – Ismail Farooq Jun 06 '16 at 12:04
  • Exactly, but once im on the page and click on the menu Link with the Anchor it works. So i need to simulate that Menu click in js. – Mella Jun 06 '16 at 12:06
  • @ismail could be, but its also happening in ff. I could try the linked Code, but I'd still need the "Grab Anchor from url and go to that specific Anchor" functionality... – Mella Jun 06 '16 at 12:20

1 Answers1

0

I think this should work in every browser - what happens to be the problem?

In order to achieve this in jquery you should scroll to the element/anchor with javascript as soon as the document is loaded.

So like this:

$(function() {
   location.hash = "#" + hash;
});

I still think you should find out what went wrong and why the linken from another page doesn't work in some browser before using a workaround for the problem. Your code will just ged more and more messy like that.

How to scroll HTML page to given anchor using jQuery or Javascript? and here $(document).ready shorthand

Community
  • 1
  • 1
juli0r
  • 66
  • 9
  • 1
    Well I know and you're surely right, but this would be beyond my scope, so its either non working anchors or more Code. I can live with he latter. Theme author says "contact Plugin author" and now you can guess what the plugin author says.... exactly the same vice versa – Mella Jun 06 '16 at 12:10