Harsha M V
Harsha M V

Reputation: 54979

Loading Javascript on a page

I am loading jQuery and all the plugin libs at the end of the page just before the close of the body.

There are a few elements in the page that need jquery plugins.

Do i need to add the jquery config code under the document ready in the header or can it load any where ?

i am getting error that the particular function is not working because the lib is being loaded after the config part.

Whats the best solution for this ?

Upvotes: 0

Views: 72

Answers (4)

Jesse Bunch
Jesse Bunch

Reputation: 6679

I usually load all my scripts at the end of the page like you. Something like this:

        <script src="/js/jquery-1.6.2.js" type="text/javascript"></script>
        <script src="/js/jquery.ui.core.js" type="text/javascript"></script>
        <script src="/js/jquery.ui.widget.js" type="text/javascript"></script>
        <script src="/js/jquery.ui.accordion.js" type="text/javascript"></script>
        <script src="/js/jquery.ui.datepicker.js" type="text/javascript"></script>
        <script src="/js/jquery.isotope.js" type="text/javascript"></script>
        <script src="/js/jquery.ready.js" type="text/javascript"></script>

    </body>
</html>

You should load all your dependencies first. jQuery is usually first, followed by all your plugins, then by your document.ready code. If you have Javascript code embedded into the page, you'll need to either move all your scripts to the header, or (more preferable) move all your inline code to an external file. See my blog post on how I keep it all organized.

Hope that helps.

Upvotes: 1

kleinohad
kleinohad

Reputation: 5912

the best practice is to load the the js file in the head section at the beginning of the page

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
.....
</head>

also if for example A script uses B script it should be called first

Upvotes: -1

ShankarSangoli
ShankarSangoli

Reputation: 69915

You should load the core jquery js before you start using it like $(document).ready();

Upvotes: 2

davecoulter
davecoulter

Reputation: 1826

Page resources are loaded in order. Is there a performance reason why you aren't just declaring those plugins at the beginning of the document?

Upvotes: 1

Related Questions