javascript - Can't insert js programmatically if it uses document.write - Stack Overflow

I am trying to insert js files programmatically, using jquery and something like this:var script = docu

I am trying to insert js files programmatically, using jquery and something like this:

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://someurl/test.js';
$('body').append(script);

It works fine, if test.js contains an alert or some simple code it works fine, but if the file test.js contains document.write, and the file including the js is hosted on another domain than test.js (or localhost), nothing happens and firebug shows the error :

A call to document.write() from an asynchronously-loaded external script was ignored.

If the test.js and the file that include it are hosted on the same domain, on chrome it still wont work but on firefox the document.write gets executed fine but the page stays "loading" forever and sniffer show request to all the files with "pending" status.

What other methods to include js files programmatically could I try?

I am trying to insert js files programmatically, using jquery and something like this:

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://someurl/test.js';
$('body').append(script);

It works fine, if test.js contains an alert or some simple code it works fine, but if the file test.js contains document.write, and the file including the js is hosted on another domain than test.js (or localhost), nothing happens and firebug shows the error :

A call to document.write() from an asynchronously-loaded external script was ignored.

If the test.js and the file that include it are hosted on the same domain, on chrome it still wont work but on firefox the document.write gets executed fine but the page stays "loading" forever and sniffer show request to all the files with "pending" status.

What other methods to include js files programmatically could I try?

Share Improve this question asked Jan 6, 2012 at 3:56 Trevor SkiTrevor Ski 932 silver badges7 bronze badges 1
  • 2 The problem isn't how you're including the file, it is that there is a document.write() being executed after the page has loaded. – nnnnnn Commented Jan 6, 2012 at 4:03
Add a ment  | 

4 Answers 4

Reset to default 4

use innerHTML instead of using document,write.

and use following code to register script,

(function() {
    var jq = document.createElement('script');
    jq.type = 'text/javascript';
    jq.async = true;
    jq.src = 'http://someurl/test.js';
    var s = document.body.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(jq, s);
})();

Document.write is ONLY for synchronous tasks when the html is loaded (for the very first time), never for asynchronous tasks like the one you are trying to do.

What you want to do is dynamically insert a <script> DOM element into the HEAD element. I had this script sitting around. As an example, it's a race condition, but you get the idea. Call load_js with the URL. This is done for many modern APIs, and it's your best friend for cross-domain JavaScript.

<html>
    <head>
        <script>
            var load_js = function(data, callback)
            {
                    var head = document.getElementsByTagName("head")[0];

                    var script = document.createElement("script");
                    script.type = "text/javascript";
                    script.src = data;
                    head.appendChild(script);

                    if(callback != undefined)
                            callback();
            }

            load_js("http://ajax.googleapis./ajax/libs/jquery/1.6.2/jquery.min.js");

            setTimeout(function() {
                $('body').html('loaded');
            }, 1000);
        </script>
    </head>

    <body></body>
</html>

There isn't anything wrong with your approach to inserting JavaScript. document.write just sucks a little bit. It is only for synchronous tasks, so putting a document.write in a separate script file is asking for trouble. People do it anyway. The solution I've seen most often for this is to override document.write.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744409484a4572802.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信