0

I am new to JS and programming in general and hope someone can help me with this.

I am currently working on building a website where every page has its separate HTML / PHP file. jQuery and my global / general JS functions are included in the footer of all these pages through a separate includes file "footer.php".

So far everything works as intended.

Now I have some pages that will use specific jQuery functions so I want to load the corresponding JS only if such a page is loaded.

To do this I saved the corresponding codes in separate JS files (e.g. nameOfExternalJsFile.js) and wrapped everything in there in the following:

$(document).ready(function(){
   // ...
}); 

I then made the following updates to the corresponding PHP pages (example):

<head>
    <?php 
        require_once("includes/header.php");
    ?>

    <!-- here I want to include the specific jQuery functions -->
    <script src="includes/js/nameOfExternalJsFile.js"></script>
</head>
<!-- ... -->
<!-- here I include the main JS functions -->
<?php require_once("includes/footer.php"); ?>

I have two concerns with this:

  1. I am not sure if this is the right way of including such files since I need to have them available on document ready.
  2. I include my main JS in the footer since I was told this improves performance but can I then include jQuery functions in the header at all ?

Can someone let me know if this is correct or if I should change anything here ?

Many thanks for any help with this.

4 Answers 4

3
  1. Wrapping the functions in $(document).ready automatically takes care of this concern. From the JQuery documentation on document.ready.

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

  1. Technically it doesn't matter whether you include the scripts in the header or the footer, as long you load JQuery first and your script second.

    That said, it's generally recommended that both scripts go just before the closing body tag to increase performance as you suggested. There are some articles that discuss this like this post from performance expert Steve Souders and this guide from the Yahoo! Exceptional Performance team.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for this ! The PHP footer include is the last line before I close the body tag so this should be ok then, right ? Regarding the document ready line, I can keep this on the external file as long as I include jQuery first and then this external file, correct ?
Yes on both counts :-)
1

You should load the $(document).ready(...) stuff only after you have loaded jQuery, that is, in the footer file, after the jQuery <script> tag, like this :

<script src="includes/js/jQuery.min.js"></script>
<script src="includes/js/nameOfExternalJsFile.js"></script>

Comments

1

It`s good practise to locate all the JS files in the end of the body

<html>
<head></head>
<body>
... Some HTML
<script>SomeScripts</script>
</body>
</html>
</pre>

If you want to be sure that your external scripts are loaded after page load use: $(function(){ /* Your code from the scripts */ });

Comments

0

You can change the content of footer.php to include /nameOfExternalJsFile.js manually at the bottom of the page. That´s the safest way to do it because you may load jquery before loading others scripts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.