Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  81] [ 1]  / answers: 1 / hits: 41983  / 12 Years ago, mon, march 5, 2012, 12:00:00

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?


More From » javascript

 Answers
74

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.


[#87043] Saturday, March 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dasias

Total Points: 344
Total Questions: 100
Total Answers: 100

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;