PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

How to remove form javascript?

  1. WorldWideWebb
    Member

    How do I suppress the javascript (it's jQuery-specific code) that's output with each form? Even better, how do I move that code block to the footer? I'm pretty adamant about jQuery being referenced only in the footer (for performance reasons, and so my page load isn't blocked while it's being downloaded).

    Thx

    Posted 11 years ago on Saturday September 8, 2012 | Permalink
  2. WorldWideWebb
    Member

    For all those interested, I hacked together my own solution for this. Seems to work well if you're embedding forms via a function call (which I am in the case of my general contact form):

    include('/simple_html_dom.php');
    ob_start();
    gravity_form(1, false, false, false, '', true);
    $gravity_form_output = ob_get_contents();
    ob_end_clean();
    
    $gravity_form_clean = str_get_html($gravity_form_output);
    $gravity_form_js = '';
    
    foreach($gravity_form_clean->find('script') as $script_tag) {
    	$gravity_form_js .= $script_tag;
    	$script_tag->outertext = '';
    }
    
    echo $gravity_form_clean;

    Then, in the footer (after all of your other javascript), you echo the stripped javascript back:

    echo $gravity_form_js;

    All this requires is the PHP Simple HTML DOM Parser, which gives us an easy way to strip out HTML nodes. It can be found here: http://simplehtmldom.sourceforge.net/

    I'm sure this is kinda hacky and definitely isn't supported, but it seems to work (Gravity guys, I'd love to hear if this might cause any issues that you can think of), prevents me from having to touch the plugin so that it can be upgraded properly, and allows me to move jQuery back to the footer.

    Posted 11 years ago on Sunday September 9, 2012 | Permalink