简体   繁体   English

Wordpress + Gravity Forms Ajax + Swup JS

[英]Wordpress + Gravity Forms Ajax + Swup JS

I am using swup.js for page transition and changing content via AJAX.我正在使用 swup.js 进行页面转换和通过 AJAX 更改内容。 Unfortunately Gravity forms does not seem to be handling the AJAX request well and my form is not working after transitioning.不幸的是,重力表单似乎不能很好地处理 AJAX 请求,并且我的表单在转换后无法正常工作。

I am using the following to reinitialize my scripts on page load:我正在使用以下内容在页面加载时重新初始化我的脚本:

document.addEventListener('swup:pageView', (event) => {
// code 
})

Is there any function I could reinitialize in javascript so Gravity forms would reinitialize on each page transition?是否有任何我可以在 javascript 中重新初始化的函数,以便 Gravity 表单会在每个页面转换时重新初始化? Could not find anything in the documentation.在文档中找不到任何内容。

I know the date of the post is far, but this question just popped to me and in case you could not find the answer,我知道帖子的日期很远,但这个问题突然出现在我面前,万一你找不到答案,

try using this code, this will watch any changes applies to the DOM of the body and will trigger whatever you want:尝试使用此代码,这将观察适用于主体的 DOM 的任何更改,并将触发您想要的任何内容:

<script>
    $(document).on('DOMSubtreeModified', 'body', function (el) {
        console.log('body content changed');
        // append you init code here
    });
</script>

Tell me if this is working or not.告诉我这是否有效。

Ended up using SwupFormsPlugin and Contact Form 7 with the following最终使用 SwupFormsPlugin 和 Contact Form 7 与以下内容

document.addEventListener('DOMContentLoaded', function (event) {
  var domain = window.location.origin // HACK: makes link selector relative
  var isMobile = window.innerWidth < 800
  if (isMobile) {
  } else {
    var swup = new Swup({
      plugins: [
        new SwupOverlayTheme({
          color: '#eff6fb',
          duration: 3000,
          direction: 'to-bottom',
        }),
        new SwupBodyClassPlugin(),
        new SwupFormsPlugin({ formSelector: 'form.wpcf7-form' }),
        new SwupScrollPlugin({
          doScrollingRightAway: false,
          animateScroll: true,
          scrollFriction: 0.3,
          scrollAcceleration: 0.04,
        }),
      ],
      FORM_SELECTOR: 'form.wpcf7-form',
      LINK_SELECTOR: 'a[href*="'.concat(
        domain,
        '"]:not([data-no-swup]), a[href^="/"]:not([data-no-swup]), a[href^="#"]:not([data-no-swup]), a[xlink\\:href]'
      ),
    })
    swup()
  }
})
function domReady (callback) {
  document.readyState === 'interactive' || document.readyState === 'complete'
    ? callback()
    : document.addEventListener('DOMContentLoaded', callback)
}
export { domReady }

Home it helps anyone家,它可以帮助任何人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM