• Resolved freshcreate

    (@freshcreate)


    Hi there, thanks for a great plugin πŸ™‚

    I’m trying to use window.location.hash for some function, for a hash that happens to also be an anchor link. The hash value is coming up blank. When I disable your plugin, the value does appear. Also – when the hash value is NOT also an element ID on the page, window.location.hash does work.

    Here’s what I’m using to test:

    	var hash = window.location.hash;
    	console.log(hash);

    Thanks in advance for any advice.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author malihu

    (@malihu)

    Hello,

    This happens because plugin’s “Scroll from/to different pages” option is enabled.

    This option manipulates window.location.hash value as soon as the page is ready, in order to scroll to the target smoothly later.

    If you disable/uncheck this option, your code will work.
    If you still want this option enabled, you’d have to delay your code and make it run when the page is fully loaded (e.g. $(window).on("load") in jQuery).

    What currently happens is your code executes while “Page scroll to id” manipulates window.location.hash value (it removes it and add it back again when page is fully loaded). So when you try to get the hash value, it’s no longer there (it gets back later when page is completely loaded).

    The plugin script works this way because it’s the only way to prevent the browser from jumping to a target instead of scrolling smoothly to it.

    Hope all this makes sense πŸ™‚

    Thread Starter freshcreate

    (@freshcreate)

    Perfect! Onload didn’t help, but setting a delay of 1s did.

        setTimeout(function(){
            var hash = window.location.hash;
            console.log(hash);
        }, 1000);

    Many thanks!!!

    Thread Starter freshcreate

    (@freshcreate)

    I have a second question for you, in extending this solution further:

    I’m trying to make use of the data-ps2id-offset attribute, applied as a custom offset amount for the links that use the delay solution we discussed above.

    Experiencing a similar issue: when clicking these links from the same page, the custom offset is applied correctly… but when clicking them from a different page, the default offset amount is the one applied.

    Any advice on this one?

    Plugin Author malihu

    (@malihu)

    Unfortunately, this is a current plugin limitation. Custom attribute offsets do not apply when scrolling to a target on page load.

    Is it essential to use the data-ps2id-offset on your links? If you can post your link, I could check it and might be able to suggest another solution/workaround.

    Thread Starter freshcreate

    (@freshcreate)

    Got it – thanks for the heads up on that.

    Appreciate the offer but I can’t quite send a link since the site is in development and hidden from public.

    I thought I found a workaround. My initial impression was that I had two groups: all non-delayed links would require the same offset amount, and delayed links would all require their own offset amount. So my workaround was to set data-ps2id-offset for the non-delayed links and have the default be the delayed link offset amount. I had it configured the opposite way.

    But as it turns out, I have different subsets in the non-delayed links, depending on their parent nav item, each grouping requires a different subset amount. I’m afraid this is become a little too dynamic for my own good πŸ™‚ But I really wouldn’t want to sacrifice the smooth scroll functionality that you offer.

    It becomes even more complicated by the fact that the delayed links are navigation items, so I had to add a function to add the data-ps2id-offset attribute, like so:

    add_filter( 'nav_menu_link_attributes', 'extra_atts', 10, 3 );
    function extra_atts( $atts, $item, $args )
    {
        $atts['data-ps2id-offset'] = '134';
        $atts['rel'] = 'm_PageScroll2id';
        return $atts;
    }

    I do believe I’d be able to set attributes to nav items based on their parent, but I can’t pull it off unless I can take advantage of data-ps2id-offset on delayed links.

    I’m close to giving up, but figured I’d run my scenario by you and see if you have any ideas.

    Plugin Author malihu

    (@malihu)

    I can’t really say… It sounds like a complicated(?) layout so I’d need to see what exactly happens and maybe suggest a solution.

    With javascript you could change the offset dynamically but I don’t know if this would even help. Maybe adjusting your targets padding might do the trick(?)

    I understand that your site is in development but it’s impossible for me to suggest a solution or a workaround. Maybe you could create a temporary user account so I could check it?

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Seems incompatible with window.location.hash?’ is closed to new replies.