Attach the <script> tag to the head

Attach the <script> tag to the head

Hi fellas!

I have a problem with adding some script files to my page. I update all the content on the page via ajax, so if on a new page I have some new js/css - files I haven't on previous, I need to attach 'em. In success method:

  1. // Attach the css files
  2. if ( css.length > 0 ) {
  3.     for ( var i in css ) {
  4.         if ( $('link[href="' + css[i] + '"]').length == 0 ) {
  5.             $('<link />', {
  6.                 href : css[i],
  7.                 media : 'all',
  8.                 rel : 'stylesheet'
  9.             }).appendTo('head');
  10.         }
  11.     }
  12. }

  13. // Attach the js files
  14. if ( scripts.length > 0 ) {
  15.     for ( var i in scripts ) {
  16.         if ( $('script[src="' + scripts[i] + '"]').length == 0 ) {
  17.             $('<script/>', {
  18.                 src : scripts[i],
  19.                 type : 'text/javascript'
  20.             }).appendTo('head');
  21.         }
  22.     }
  23. }

The css files has added as excected, but the script files has not. Instead of that I had the GET requests to each of the files.