Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So normally javascript files are loaded using the tag.

But how can you bind the loading of scripts files(For some secondary features like searching) to the document ready event?

And is it generally a good way to minimize your page's response time?

(sorry maybe I should ask on SO, but I do want to hear from people that mange the server.)

share|improve this question

closed as off topic by Zoredache, Magellan, rnxrx, Iain Oct 15 '12 at 20:02

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
i think it is using the $.getScript('xxx.js'); function. Anyway further comments are welcomed. –  StCee Oct 12 '12 at 17:52
    
If you look at the right site, and actually do a search there you will see there are lots of questions already covering this ground. stackoverflow.com/search?q=load+javascript+runtime –  Zoredache Oct 12 '12 at 18:31

1 Answer 1

You can do this by javascript. Basically you're modifying the DOM to add a javascript source at runtime.

If you're using Google Analytics, you can see that they are using this for adding the necessary JS code to the page:

<script type="text/javascript">
...

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.