php - What is the best practise for loading extra JavaScript in the footer? -
every page of website consist of:
- generic header
- the page itself
- generic footer
in code page looks this:
<?php require_once('header.php'); load_header('title', 'meta description'); // loads menu ?> // unique content of page <?php // footer settings $load_contact_info = true; // load footer require_once('footer.php'); load_footer($load_contact_info); ?>
on same pages load content (like more js libraries) in footer.php
(right before - closing body tag):
<?php function load_footer($load_contact_info = false) { ?> <footer> <?php if ($load_contact_info) { ?> <div id="contact-us"> // contact info </div> <?php } ?> <div id="inner-footer"> // more generic footer content </div> </footer> <script src="<?php echo($url); ?>js/vendor/jquery.js"></script> // want load more unique js content here @ pages </body> </html> <?php } ?>
what best practise loading libraries in footer? should extend footer.php
more if-yes-load-more this:
if ($google_maps_api) { // map settings echo "<script src=\"https://maps.googleapis.com/maps/api/js?callback=initmap\" async defer></script>"; } if ($other_api) { echo "<script src=\"https://example.com/other-api/\" async defer></script>"; }
or there other possibilites better?
load libraries should not task footer. footer displays footer. should create new php file , name "handle_js.php"
function print_script_tag($src, $options) { echo "<script src=\"" . $src . "\" " . $options . "></script>\n"; } function print_all_script_tags_for_url($url) { if($url === 'example') { print_script_tag('library.js','async'); } }
and in footer file
</footer> print_scripts($url); </body>
i didn't test code.
Comments
Post a Comment