defunkt / jquery-pjax

pushState + ajax = pjax
https://pjax.herokuapp.com
MIT License
16.73k stars 1.97k forks source link

Difference between PJax defaults (PHP widget and JQuery) #630

Closed almirb closed 7 years ago

almirb commented 8 years ago

Hi!

I'm using pjax for erasing values of my gridview. But after the request the page always scrolls to top. That's annoying.... After a little research, I discovered I could overcome the problem with

$.pjax.defaults.scrollTo = false;

Why the Yii Pjax Widget (yii\widgets\Pjax) has the 'scrollTo' default value = false and in jquery.pjax.js the defaults are definied with 'scrollTo: 0'?

In jquery.pjax.js (around line 920):

// Install pjax functions on $.pjax to enable pushState behavior.
//
// Does nothing if already enabled.
//
// Examples
//
//     $.pjax.enable()
//
// Returns nothing.
function enable() {
  $.fn.pjax = fnPjax
  $.pjax = pjax
  $.pjax.enable = $.noop
  $.pjax.disable = disable
  $.pjax.click = handleClick
  $.pjax.submit = handleSubmit
  $.pjax.reload = pjaxReload
  $.pjax.defaults = {
        history: true,
        cache: true,
    timeout: 650,
    push: true,
    replace: false,
    type: 'GET',
    dataType: 'html',
    scrollTo: 0,       <----- HERE
    maxCacheLength: 20,
    version: findVersion,
    pushRedirect: false,
    replaceRedirect: true,
    skipOuterContainers: false,
    ieRedirectCompatibility: true
  }
  $(window).on('popstate.pjax', onPjaxPopstate)
}

Thanks in advance!

P.S.: I also created a post in Yii Forum.

socalengineer commented 8 years ago

This doesn't look like a bug or issue to me, different people have different tastes. Set your defaults to what you like them to be.

mislav commented 7 years ago

The default is scrolling to the top because that's how browser navigation usually works: when you change the page, you're brought to the top of the new page. However, you can change the default yourself, as you already found out, or you can specify the scrollTo value for every individual $.pjax call if you need to.