Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

share|improve this question
    
Are you trying to inject script on runtime? – rahilwazir Jan 2 at 11:02
    
Yes :) @RahilWazir – Tommy Jan 2 at 12:08

3 Answers 3

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

$.getScript("http://my/script/url.js");
share|improve this answer
    
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 5 at 13:02
up vote 1 down vote accepted

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>
share|improve this answer

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.

share|improve this answer
    
It creates the <script> element, but does not load the script. Thanks anyway, it was a good simpel solution. – Tommy Jan 2 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 2 at 12:22
    
@TommyJepsen It should be text/javascript – rahilwazir Jan 2 at 12:50
    
Well, when I programmatically put it in a <script type="text/javascript">, it works, but crashes my whole application. @user860478 – Tommy Jan 2 at 13:20
    
Thanks @RahilWazir but still no success. It doesn't visualize anything. – Tommy Jan 2 at 13:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.