Skip to content

Instantly share code, notes, and snippets.

@jaclyntan
Last active June 30, 2020 07:41
Show Gist options
  • Save jaclyntan/dd4a955fb89eeac07f57e359c9c9f9d6 to your computer and use it in GitHub Desktop.
Save jaclyntan/dd4a955fb89eeac07f57e359c9c9f9d6 to your computer and use it in GitHub Desktop.
Snippet to get the next page using ajax in WordPress
/*!
♡♡♡♡♡♡♡♡♡♡♡
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
Async functionality
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
♡♡♡♡♡♡♡♡♡♡♡
*/
$(document).on('click', '#page a:not([target="_blank"]):not([href="#"]):not(.btn), [href^="/"]', function (e) {
var $this = $(this),
$page = $this.attr('href'),
pathname = this.pathname;
e.preventDefault();
$('body').removeClass('page-loaded').addClass('page-loading');
$.get({
url: $page,
cache: true
}, function (data) {
var $items = $(data).find('#page-wrapper'),
title = $(data).filter('title').text();
// console.log($items);
// html = $(data).html;
let parser = new DOMParser(),
doc = parser.parseFromString(data, "text/html"),
// body classes
docTheme = doc.body.getAttribute('data-themeify'),
docClass = doc.body.getAttribute('class');
// Garbage collection, you don't need this anymore.
parser = doc = null;
// history.pushState({"html":html}, '', $page);
history.pushState('', '', pathname);
// windowHeight = $(window).height();
closeMenu();
//can wait til images are loaded, otherwise just render the page as it loads
// $items.imagesLoaded().always(function (instance) {
new Promise(function (resolve, reject) {
let $content = $items.html();
document.title = title;
$('#page-wrapper').empty();
$('#page-wrapper').append($content);
resolve();
}).then(function (result) {
$('.site-branding a').blur();
return new Promise((resolve, reject) => {
resolve();
});
}).then(function (result) {
$('body').removeAttr('class').addClass(docClass);
$('body').addClass('page-loaded');
$('body').attr("data-themeify", docTheme);
$('li.menu-item').removeClass('focus').blur();
$('html').scrollTop(0);
//reactivate js
animateIn();
slider();
activateSplash();
contactPage();
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(smoothScroll);
return new Promise((resolve, reject) => {
resolve()
});
});
});
});
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
//change url when new page is loaded
window.onpopstate = function (e) {
window.location.href = window.location.href;
// if(e.state){
// window.location.href = window.location.href;
// }
// console.log(e.state);
// window.location.href = window.location.href;
};
//back to top
// $('.to-top').on('click', function (e) {
// $('html, body').animate({scrollTop: '0px'}, 700);
// });
//add active classes to menu when async is active
$('#primary li').on('click', function (e) {
$('#primary li').removeClass('current_page_item').removeClass('current-menu-item');
$(this).addClass('current_page_item');
if ($(window).width() < 1024 && 'ontouchstart' in window && !this.classList.contains('current_page_item')) {
closeMenu();
}
});
$('.site-branding a').on('click', function (e) {
$('#primary li').removeClass('current_page_item').removeClass('current-menu-item');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment