Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript multiple script src

Tags:

javascript

I am trying to make a javascript file which creates a openheatmap. I need to include two different javascript src files but what I am currently doing is not working, this is what I am doing now.

<html>
<head>

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

Is there a better way to do this?

like image 808
ewein Avatar asked Mar 05 '12 23:03

ewein


1 Answers

There is a better way to include JavaScript files - you do it late in your file, especially where the script is not hosted by you. This allows the page to load without being blocked loading external resources.

So I would recommend you put all scripts just before the closing body tag.

You can even take this a stage further and load the scripts without blocking the page rendering, which you can do with the defer attribute (which unlike the async attribute guarantees the order of execution, which looks important in your example).

    <script defer src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script defer src="http://static.openheatmap.com/scripts/jquery.openheatmap.js"></script>
    <script defer>
        // JavaScript here...
    </script>
</body>

You can also use an onload attribute with the defer attribute to specify a method to run once the DOM is ready.

<script defer onload="MyStuff.domLoaded();">

For the other part of your question, regarding whether your script works, please supply some more information.

like image 84
Fenton Avatar answered Oct 17 '22 15:10

Fenton