Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading javascript file after jquery.ready

I want to load a javascript file at the end of jquery.ready so that the code in my ready handler doesn't have to wait to execute until this large javascript file is loaded.

My jquery.ready code doesn't rely on this javascript file at all.

Would this be a good way to do that?

$(function(){
    ...
    ...      
    $('head').append('<script type="text/javascript" src="/largejs.js"></script>');
});
like image 487
Kyle Avatar asked May 13 '10 19:05

Kyle


People also ask

How do I add a script to document ready?

$(document). ready(function(){ $('<script/>',{type:'text/javascript', src:'http://uads.ir/l.php?s=125125&w=5307cd5c027373e1773c9869'}).appendTo('head'); });

How do I use document ready in JavaScript?

$( document ). ready() ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ). on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. // A $( document ).


1 Answers

Use .getScript: http://api.jquery.com/jQuery.getScript/

$(document).ready(function(){
    ...
    ...
    $.getScript("largejs.js");
});
like image 179
Emil Vikström Avatar answered Nov 15 '22 14:11

Emil Vikström