javascript - Script/Links tags vs AJAX loading -


i want load script tags , link tags. can follows

 <script src="sample.js" type="text/javascript"></script>  <link type="text/css" href="sample.css" rel="stylesheet"></link> 

(or)

 $.ajax({     url: 'sample.js',     datatype: 'script',     success: function(data) {},     error: function(data) {}  }); 

but want know 1 best/secure way[generally].

i think can handle errors ajax loading or else have check onload function find whether scripts loaded , find whether links loaded need loop through document.stylesheets.

any suggestions?

my main concern find whether scripts & links loaded , secured way , testing don't prefer code this.

as javascript files:
javascript has blocking nature. if going load multiple external js files on page 1 one, consider each loaded , executed. if error occurs while execution of javascript file can observe in developer console. unfortunately, javascript downloads still block downloading of other resources.

conclusion: place <link> tags before <script> tags. it’s recommended place <script> tags close bottom of <body> tag possible



as css files: check if stylesheet loaded little more in affair. here check existing(not loaded) css file jquery:

if (!$("link[href='sample.css']").length){     console.log('error while downloading sample.css');     $('<link href="sample.css" rel="stylesheet">').appendto("head"); } 

to check if css file loaded, use currentstyle or window.getcomputedstyle methods in pure javascript , search element known class or identifier (which exists in css file)


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -