问题描述
我目前正在开发一个Squarespace网站,试图找出如何将一个函数绑定到他们的AjaxLoader而不会取得多大成功。
I'm working on a Squarespace site at the moment, trying to figure out how I can bind a function to their AjaxLoader without much success.
这是一个
In我的控制台我可以看到在页面之间切换时触发的所有XHR请求,并且我已经尝试使用调试来捕获事件监听器。
In my console I can see all the XHR requests being triggered when switching between pages, and I've tried using the debug to capture event listeners.
我认为某些事情必然会 readystatechange会工作,但它只会在初始页面加载时触发。
I thought something bound to "readystatechange" would work, but it only triggers on the initial page load.
$(window).on("readystatechange", doStuff);
推荐答案
我在Squarespace上使用mutationObservers找到了一个非常好的解决方案答案论坛 - 谢谢你!
I found a pretty nice solution using mutationObservers on the Squarespace Answers forum – thank you @octopus!
从
希望这可以帮助其他人挣扎。
Hope this helps anyone else struggling.
window.onload = function(){
react();
watch(react);
};
function react() {
var id = document.body.getAttribute('id'),
target = 'collection-5787c6b0cd0f6801e859286a';
console.log(id);
if (id === target) {
// execute your code for this page
}
}
function watch(callback) {
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations) {
for (var index = 0; index < mutations.length; index++) {
var mutation = mutations[index];
if (mutation.type === 'attributes') {
callback();
}
}
});
observer.observe(document.body, {
attributes: true,
attributeFilter: ['id']
});
}
这篇关于如何将事件绑定到Squarespace AjaxLoader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!