How to lazy load scripts in AngularJS app?

Some time ago I was having a problem with JS loading from partials in an AngularJS app and today I decided to share my solution with you. The problem is that when web page is loaded, javascript libs in partials are not being loaded. You have to force them to load. You can do this with some imagination, a directive and some jQuery (the last one is optional). 🙂

app.directive('script', [function() {
  function load_script(src, identifier) {		
    var s = document.createElement('script');
    s.type = 'text/javascript';
		
    if(identifier !== undefined) {
      // if element with some identifier exists, remove it                         
      jQuery('script[data-identifier="' + identifier + '"]').remove(); 
      // add an identifier to the new script object so we can remove it if needed
      s.setAttribute('data-identifier', identifier);
    }	
		     
    if(src !== undefined) {		
      s.src = src;
    }
    else {
      var code = elem.text();
      s.text = code;
    }

    document.body.appendChild(s);
  }

  return {
    restrict: 'E',
    scope: false,
    link: function(scope, elem, attr) {
      if (attr.type=='text/javascript-lazy') {
        load_script(elem.attr('src'), elem.attr('data-identifier'));
        elem.remove();
      }
    }
  };
}]);

When partial is loaded the directive happens for each sript object in that partial. If script is of type text/javascript-lazy, load_script function is called. This function creates new element of type text/javascript and adds src or code of lazy element to the new element. All elements with specific (custom) identifier are then removed from DOM and new element is appended to body. When appended to body, script gets forcefully loaded.

Error when opening explorer: “Server execution failed error.”

Recently I had a problem with explorer on Windows 7 – when coworker tried to open explorer when not on local network, “Server execution failed” error happened. I was suspecting that the problem might be connected to mapped disks which were not accessible, when not on local network, but wasn’t sure how this could lead to such an obscure error. After some heavy duty browsing I found a solution: the problem was in “Launch folder windows in a separate process” setting. This setting can be found somewhere in Folder Options window and it should be unchecked on Windows 7. By default this setting is not checked, because it can eat more RAM and can slow down your computer. But it also says that it could increase Windows stability (when error in one process occurs other processes are not affected). But obviously there is a bug in Windows 7, because everything stops working when not in domain. 🙂 In Windows 8.1 I didn’t detect this problem. Well, problem solved – yipikaye motherf****r. 🙂