0

In my one of the PHP project, part of it done by some other people. All the js scripts are loaded in footer file. The html closes at the footer file. How Can I use jquery functions before the inclusion of footer file ?. Since using it after the footer file will show it after the closing of html. When I tired to use jquery functions before the footer I get the js error $ not defined etc.. I have searched many similar questions but couldn't find a solution. I can't change the file inclusion order currently since some other people have done it. Any help will be appreciated.

7
  • 1
    You cannot use jQuery before the <script> tag that imports jQuery. Commented Jun 25, 2015 at 17:54
  • 1
    You could put your code in an external .js file, and load it from the same footer code that loads jquery. Commented Jun 25, 2015 at 17:56
  • 1
    Just include every Javascript in your header... (like almost every website do) Commented Jun 25, 2015 at 17:56
  • 2
    @PLAudet actually it's suggested (and it's a really great idea) to put your script tags before the </body> closing tag. Commented Jun 25, 2015 at 17:59
  • 2
    Actually, it is best practice now to put them in the header and use the async and defer attributes. See this question and this link from google about blocking js. Commented Jun 25, 2015 at 18:05

3 Answers 3

2

If, like you mention, you have absolutely no control over where jQuery scripts are included, you can set timer to check when jQuery is ready, e.g.:

<script>
  var i = setInterval(function() {
    if ($) {
      clearInterval(i);
 
       $(document).ready(function(){
          alert('Ready!');
       })        
    }
  }, 100)
  
</script>  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

But otherwise - if you can control it - include them before your code.

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

Comments

1

this code will only run properly in modern browsers (IE > 8)

<div class="jq"></div>
<script>
document.addEventListener('DOMContentLoaded', function(){
    $('.jq').html('working');
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Example - http://jsfiddle.net/2mmdrega/

Comments

1

I sometimes did something like this:

    function JQueryManager(callback, thisArgument) {

        this.execute = function() {
            if (typeof jQuery == "undefined") {
                console.log("Waiting for jQuery...");
                var self = this;
                var selfArgs = arguments;
                setTimeout(function() {
                    self.execute(selfArgs)
                }, 100)
            } else {
                console.log("Found jQuery. Version: " + jQuery.fn.jquery);
                callback.apply(thisArgument, arguments)
            }

        }
    }

    function myCallback(results) {
        alert("found " + results.length + " results")
    }


    var jQueryManager = new JQueryManager(myCallback);
    jQueryManager.execute($("div"));

That allows you to execute a callabck function when jQuery is available

EDIT: fixed some copy-paste errores

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.