【问题标题】:How to move the inline jQuery script of Ajax Gravity Form to the footer?如何将 Ajax Gravity Form 的内联 jQuery 脚本移动到页脚?
【发布时间】:2020-07-13 20:21:40
【问题描述】:

为了添加延迟到jquery链接,我想将Ajax Gravity Form的内联jQuery脚本移动到页脚。

我该怎么做?

【问题讨论】:

    标签: wordpress jquery-deferred gravity-forms-plugin critical-css


    【解决方案1】:

    这对我有用。我希望这也对你有用。

    // Force Gravity Forms to init scripts in the footer and ensure that the DOM is loaded before scripts are executed
    add_filter( 'gform_init_scripts_footer', '__return_true' );
    add_filter( 'gform_cdata_open', 'wrap_gform_cdata_open', 1 );
    function wrap_gform_cdata_open( $content = ” ) {
        if ( ( defined('DOING_AJAX') && DOING_AJAX ) || isset( $_POST['gform_ajax'] ) ) {
            return $content;
        }
        $content = 'document.addEventListener( "DOMContentLoaded", function() { ';
        return $content;
    }
    add_filter( 'gform_cdata_close', 'wrap_gform_cdata_close', 99 );
    function wrap_gform_cdata_close( $content = '' ) {
        if ( ( defined('DOING_AJAX') && DOING_AJAX ) || isset( $_POST['gform_ajax'] ) ) {
            return $content;
        }
        $content = ' }, false );';
        return $content;
    }
    

    【讨论】:

    • 解释你的答案
    • 这个解决方案对我有用,愚蠢的是 Gravity Forms 不会考虑那些不想在头脑中加载 jquery 的人。谢谢!
    【解决方案2】:

    这可能有效:

    /**
     * Force GFORM Scripts inline next to Form Output
     *
     * force the script tags inline next to the form. This allows
     * us to regex them out each time the form is rendered.
     * 
     * see strip_inline_gform_scripts() function below
     * which implements the required regex
     */
    add_filter("gform_init_scripts_footer", "__return_false");
    
    /**
     * Strip out GForm Script tags
     *
     * note: this diables post and pre render hooks which are triggered
     * when the form renders so if you need these then it's important
     * to manually re-add them in your compiled JS source code
     */
    
    add_filter("gform_get_form_filter", function( $form_string, $form ) {
      return $form_string = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $form_string);
    }, 10, 2);
    

    【讨论】:

    • 感谢您发送消息。但是当我尝试使用您的代码时。 preg_replace 函数删除了内联脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2016-09-11
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 2018-05-16
    • 2013-02-15
    相关资源
    最近更新 更多