-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pjax + inline script + ajax #510
Comments
bug with ajax has been resolved by removing "pjax:end" from "document.ready" |
What you're experiencing in bug#1 is a valid issue of pjax-enabled sites not behaving completely the same as static sites when it comes to script loading. Please follow issue #331 (comment) for discussion and updates on the topic. This is still unsolved. The double POST requests you're getting after pjax navigation sound like double event binding discussed in #473 (comment). That is indicative of the problematic pjax setup for form submit on your site. You need to ensure that pjax will be set up only once for forms on your site and not repeated several times. So, please look at where you're setting up pjax and ensure that that setup code can't possibly get loaded and run more than once. |
@mislav thanks for answer pjax included one time (once?) on footer (footer not updating by pjax): <body>
<div id="#wrapper">
content
</div>
<script>
$( document ).ready( function() {
if ( $.support.pjax ) {
$( document ).pjax( 'a', '#wrapper', { fragment: '#wrapper' } );
}
});
</script>
</body> ajax-query of checking new messages only on one page (im) and it started by setTimeOut <body>
<div id="#wrapper">
content
<script>
function refresh() {
setTimeout( function () {
$.get(
"ajax/url",
function ( data ) {
refresh();
},
'JSON'
);
}, 7001);
}
refresh();
</script>
</div>
</body> I had to disable pjax on this page by |
OK that code helped me understand your problem a bit better. I've amended the formatting of the code examples in your previous comment. In the future, if you want to paste blocks of code in conversations on GitHub, use this syntax:
In short, surround each block of code that you paste with 3 backticks. On to your problem. I don't think I can help you much here. The problem is that you load a certain page that has inline This is your original inline script: function refresh() {
setTimeout(function() {
$.get(
"ajax/url",
function(data) {
refresh();
},
'JSON'
);
}, 7001);
}
refresh(); I propose the addition of var timer, xhr
function refresh() {
timer = setTimeout(function() {
xhr = $.get(
"ajax/url",
function(data) {
refresh()
},
'JSON'
)
}, 7001)
}
refresh()
// Handle the next pjax navigation event (either click or popstate)
$(document).one("pjax:start", function() {
// Cancel the setTimeout in progress
clearTimeout(timer)
// Cancel the Ajax request in progress
if (xhr) xhr.abort()
}) Other than this, I can't help you much with breakages in your own site's JavaScript. You'll have to handle each breakage on a case-by-case basis. Pjax sites are slightly different than regular static sites. |
I had the same issue, and this part was making the problem. When you set such a global selector as 'a' pjax acts even on ajax calls. So better idea would be to limit the scope of this selector with some parent class or with adding some custom attribute to it like [data-pjax]. Than the code would look something like this: |
Hi all!
i'm trying add pjax to existing site
This site have many inline scripts and inline ajax
bug#1: all inline script like < script scr=" somescript.js " >< / script > do not have time to load before loading < script > some javascript < / script > that is why all inline-javascript not working. But if go back to this page again - scripts will be loaded and inline-script will work. I'm using $(document).on('ready pjax:end', function()
bug#2: some pages using ajax-query for select (for example choose country, region and city) or in im-page for checking new messages. On first load page this ajax-queries not working (bug#1). On second loading this page ajax-query will work (bug#1) but thats ajax-query will doing in all other pages. But that's not all. On next viewing this page ajax-query will be duplicated.
I mean next:
First viewing page with ajax-query, console.log:
Second viewing this page, console.log:
Third viewing this page, console.log:
etc.
and this ajax-queries will do on all pages that loaded by pjax
sorry for my bad english
The text was updated successfully, but these errors were encountered: