aidanlister / jquery-stickytabs

Provides persistant state (back and forward button support) for Bootstrap tabs
http://aidanlister.com/2014/03/persisting-the-tab-state-in-bootstrap/
MIT License
60 stars 33 forks source link

#undefinded added to normal url tabs with selectorAttribute not used at all #37

Open Bhoft opened 5 years ago

Bhoft commented 5 years ago

Hi,

I have an issue that with the widget kartik-v/yii2-tabs-x/ for yii2 which uses jquery-stickytabs.

Whenever you click a tab with a normal href link (so another page is requested on tab click) #undefined is added to the current url before the actual given url of the href is requested.

I have already opened an issue there today but i think is a more on an issue of this package. See: https://github.com/kartik-v/yii2-tabs-x/issues/66

The problem is that there isn't any check if the some hash is actually given. And because of this #undefined is added if no anchor is used in the url.

This code:

 $('a', context).on('click', function(e) {
      var hash = this.href.split('#')[1];
      var adjustedhash = settings.getHashCallback(hash, this);
      changeHash(adjustedhash);
      setTimeout(backToTop, 1);
  });

doesn't check if the hash is undefined or not.

In my opinion the default getHashCallback function which is

getHashCallback: function(hash, btn) { return hash },

should be at least

getHashCallback: function(hash, btn) {if (hash === undefined) { return '';} return hash },

so #undefined isn't attached to tabs with "normal href" links. Then only only # but at least this looks better than #undefined.

Or the changeHash function should be extended to check if the given hash isn't undefined before attaching it to the url.

So a browser back would not return to the previous url with "#undefinded" attached.

I also wonder why the selectorAttribute of the link isn't added to the url e.g. via a getHashCallback function like:

function(hash, btn) {
    if (hash === undefined) {
        var btn_selector_hash = $(btn).attr(this.selectorAttribute);
        if (btn_selector_hash !== undefined) {
            return btn_selector_hash;
        }
        return '';
    }
    return hash;
}

regards Benjamin