4

I got this external JavaScript file(Ad file), which has a document.write() in it. I have tried loading it with $http, and then injecting it into a document.createElement, but the Ad server doesn't support this method; $http.get.

I have tried ng-include in a script, but no work.

I need to load the script in the file, and the run the script. How do I do it?

<script ng-src=""></script> doesn't work either. It does not run the script.

Thanks.

Tommy
  • 177
  • 2
  • 10

3 Answers3

1

If you're using jQuery (which it sounds like you are), there is a method for this:

$.getScript("http://my/script/url.js");
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thanks for replying! I got this now; Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened. Which makes me think I'm a step closer! Any ideas on how to do this? – Tommy Jan 05 '15 at 13:02
1

I ended up with this directive - helped made by my friend Dennis - that works in my situation. Hope it can help someone.

(function() {
    'use strict';

    angular
        .module('app')
        .directive('emediateScript', emediateScript);

    /* @ngInject */
    function emediateScript(Ad, $http) {
        var directive = {
            link: link,
            restrict: 'E',
            scope: {
                ad: '=ad',
            }
        };

        return directive;

        function link(scope, element, attrs){

            var _el = angular.element(element);

            var url = 'http://www.http.com';

            var request = {
                method: 'GET',
                url: url,
                headers: {
                    'X-Authentication': undefined
                }
            };

            if (url) {
                $http(request).then(function(response) {
                    var html = response.data.substr(16);
                    html = html.substring(0, html.length - 4);
                    _el.html(html);
                });
            }

        };
    };
})();

And HTML was;

<emediate-script></emediate-script>
Tommy
  • 177
  • 2
  • 10
0

Why not simply create a <script> tag with jqLite?

$('body').append('<script type="application/javascript" src="http://localhost/test.js"></script>');

It seems to work perfectly with my test file.

Victor Marchuk
  • 13,045
  • 12
  • 43
  • 67
  • It creates the – Tommy Jan 02 '15 at 12:16
  • Maybe the problem is in the script itself? Does it work for you if you add it to the html statically via script tag? Also, are there any errors in console? If you could show us the code, it would be great. – Victor Marchuk Jan 02 '15 at 12:22
  • @TommyJepsen It should be `text/javascript` – Rahil Wazir Jan 02 '15 at 12:50
  • Well, when I programmatically put it in a – Tommy Jan 02 '15 at 13:20
  • Thanks @RahilWazir but still no success. It doesn't visualize anything. – Tommy Jan 02 '15 at 13:20