javascript同步Import,同步调用外部js的方法
时间:2022-07-02 10:31:02|栏目:JavaScript代码|点击: 次
在线演示地址https://www.jb51.net/jslib/Import/a.html
主要功能代码import.js
<!--
(function(){
if(typeof window._Import != "undefined") return; //防止多次加载
function _Import(uri, x) {
var f = arguments.callee.caller;
if(typeof f == "function") {
var IsIE = (navigator.userAgent.indexOf("MSIE") == -1)? false : true;
var scriptEle = document.getElementsByTagName("head")[0].appendChild(document.createElement("script"));
scriptEle.type = "text/javascript";
scriptEle.src = uri;
if (x)
{
if (IsIE) {
scriptEle.onreadystatechange = x;
}
else {
scriptEle.onload = x;
}
}
}
}
window._Import = _Import;
})();
//-->
远程测试a.js
var a = "脚本之家www.jb51.net===";
具体调用代码
<html>
<head>
<title>-</title>
</head>
<body>
<script type="text/javascript" src="Import.js"></script>
<script type="text/javascript">
(function(){
function callback()
{
var IsIE = (navigator.userAgent.indexOf("MSIE") == -1)? false : true;
if (IsIE)
{
if(/loaded/.test(this.readyState))
{
alert(a);
}
}
else
{
alert(a);
}
}
_Import("https://www.jb51.net/jslib/Import/a.js", callback)
})()
</script>
</body></html>
主要功能代码import.js
复制代码 代码如下:
<!--
(function(){
if(typeof window._Import != "undefined") return; //防止多次加载
function _Import(uri, x) {
var f = arguments.callee.caller;
if(typeof f == "function") {
var IsIE = (navigator.userAgent.indexOf("MSIE") == -1)? false : true;
var scriptEle = document.getElementsByTagName("head")[0].appendChild(document.createElement("script"));
scriptEle.type = "text/javascript";
scriptEle.src = uri;
if (x)
{
if (IsIE) {
scriptEle.onreadystatechange = x;
}
else {
scriptEle.onload = x;
}
}
}
}
window._Import = _Import;
})();
//-->
远程测试a.js
复制代码 代码如下:
var a = "脚本之家www.jb51.net===";
具体调用代码
复制代码 代码如下:
<html>
<head>
<title>-</title>
</head>
<body>
<script type="text/javascript" src="Import.js"></script>
<script type="text/javascript">
(function(){
function callback()
{
var IsIE = (navigator.userAgent.indexOf("MSIE") == -1)? false : true;
if (IsIE)
{
if(/loaded/.test(this.readyState))
{
alert(a);
}
}
else
{
alert(a);
}
}
_Import("https://www.jb51.net/jslib/Import/a.js", callback)
})()
</script>
</body></html>
栏 目:JavaScript代码
本文标题:javascript同步Import,同步调用外部js的方法
本文地址:http://www.codeinn.net/misctech/206562.html