简体   繁体   English

在IE中加载外部js

[英]Loading external js in IE

I want to load external javascript file but I can't use AJAX requests because of same origin policy , and I have a code: 我想加载外部javascript文件,但由于具有相同的原始政策 ,因此我无法使用AJAX请求,并且我有一个代码:

<script type="text/javascript">
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'test.js';
    head.appendChild(script);
</script>

That is ok everywhere (Firefox, Chrome, Opera, IE9, IE6) but not in IE8, IE7. 到处都可以(Firefox,Chrome,Opera,IE9,IE6),但在IE8,IE7中则不行。
How can I make it work in IE8? 如何使它在IE8中工作?

How about the old document.write('<script language="javascript" src="test.js"><\\/script>') . 旧的document.write('<script language="javascript" src="test.js"><\\/script>') Also you do not have to append to head you can use body to. 您也不必附加到head即可使用body

尝试阅读有关JsonP的文章 :)

I'm not sure why you had problems in IE, but the following worked in Firefox 4, IE6 and IE9 (ie the browsers I had available): 我不确定为什么您在IE中遇到问题,但是以下内容在Firefox 4,IE6和IE9(即我可用的浏览器)中有效:

<html>
<head>

</head>
<body>
    <script type="text/javascript">
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js';
        head.appendChild(script);

        var interval = window.setInterval(function() {
            if (typeof($) !== "undefined") {
                $("<p>it worked!</p>").appendTo(document.body);
                window.clearInterval(interval);
            }
        }, 100);
    </script>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM