How to prevent javascript caching in my web form template
-
I created a web contact WP template that uses a separate javascript file for form field validation routines.
Since the javascript was only to be used on that single contact form (it is invoked via the form’s SUBMIT event) I did not want to put the SCRIPT statement in my header.php file, so I tried placing the script file at the top of my contact form’s BODY section…
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/scripts/wcontact-form.js"></script>
…and that worked very well EXCEPT that I noticed that if I ever made a change to the external .js file then some caching would occur and WordPress? would still be invoking an older version of the external .js file.
I then verified that caching was occurring by inserting a time-of-day string at the end of my script statement, as follows…
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/scripts/wcontact-form.js?<?php echo time(); ?>"></script>
…and that got around the caching issue, but for some reason it caused my external javascript validation routines to no longer work properly, so I abandoned that route and did not examine further because I then read about wp_enqueue.
According to the documentation I should be using the wp_enqueue_script function, so I then tried adding the following to the top of my contact form template…
<?php $js_loc = get_bloginfo('template_directory')."/scripts/wcontact-form.js"; wp_enqueue_script('wcontact-form', $js_loc); ?>
…but a subsequent test of my contact form showed that my form validation routines are now never invoked.
How should I add an external javascript file reference to a single WP template – and one that avoids caching issues.
BTW I have no caching plugins installed.
- The topic ‘How to prevent javascript caching in my web form template’ is closed to new replies.