袁来如此的工作笔记
袁来如此的工作笔记
竹杖芒鞋轻胜马,谁怕? 一蓑烟雨任平生。

AJAX载入外部JS文件到页面并让其执行的实例

浏览量:839

兼容所有浏览器的规范写法: 

        var script = document.createElement("script")
        script.type = "text/javascript"
        script.src = "外部JS文件的URL地址"
        //测试发现,在IE浏览器下没有script的onload事件,可能是因为加载外部JS文件时的同步特性与非IE浏览器不同所致
        //script.onload = function(){alert("Script is ready!")}
        document.body.appendChild(script)

 

AJAX载入外部JS文件到页面并让其执行的实例:


<html>
<head>
<title></title>
</head>
 
<body bgcolor="#999999">
<script id='jsobj' language='javascript' type='text/javascript' src=''></script>
<div>
<!-- saved from url=(0013)about:internet -->
<!--<p>截入需要一定的时间,请耐心等待5秒钟左右</p> -->
</div>
<DIV align="center">
<TABLE width=100% height=100% border=0 cellSpacing=0 cellPadding=0 style=margin-left:0px;margin-top:-15px;><TR><TD align=center valign=middle>
<TABLE border=1 cellSpacing=0 cellPadding=0><TR><TD>
<DIV align="center" id="flashcontent1"></DIV>
<script type="text/javascript">
    function embedflash(){
        var myswf1 = new SWFObject("http://xxx.com/sounde.swf","swf1","720","540","8","#cccccc","high");
        //myswf1.addParam("wmode", "transparent");
        myswf1.addParam("wmode", "window");
        myswf1.addParam("menu", "false");
        myswf1.addParam("swLiveConnect", "true");
        myswf1.addParam("AllowNetworking", "all");
        myswf1.addParam("allowfullscreen", "true");
        myswf1.addParam("allowScriptAccess", "always");
        myswf1.write("flashcontent1");
    }
    if(window.attachEvent&&window.ActiveXObject){
        document.getElementById("jsobj").src="http://xxx.com/swf.js"
        window.onload=function(){embedflash()}
    }else{
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://xxx.com/swf.js";
        script.onload = function(){
            //alert("Script is ready!");
            embedflash()
        };
        document.body.appendChild(script);
    }
</script>
</TD></TR></TABLE>
</TD></TR></TABLE>
</DIV>
</body>
</html>


打赏