如果延迟加载某js文件失败,便给出“加载失败”的提示语
发布网友
发布时间:2022-04-22 04:53
我来回答
共3个回答
热心网友
时间:2023-10-04 05:02
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>pro.html</title>
<style type="text/css">
</style>
<script type="text/javascript">
function loading (fPath)
{
var weather = document.getElementById ('weather');
var noweather = document.getElementById ('noweather');
var xmlhttp = loadFile (fPath);
if (!!xmlhttp)
{
var script = document.createElement ('script');
script.type = "text/javascript";
script.src = fPath;
weather.innerHTML = "";
weather.appendChild (script);
}
else
{
weather.innerHTML = noweather.innerHTML;
}
}
function loadFile (file)
{
var xmlhttp = null;
if (!!window.ActiveXObject)
{
xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (!!document.implementation && !!document.implementation.createDocument)
{
xmlhttp = new window.XMLHttpRequest ();
}
if (!!xmlhttp)
{
try
{
xmlhttp.open ("POST", file, false);
xmlhttp.send (null);
return xmlhttp;
}
catch (e)
{
return null;
}
}
return null;
};
window.onload = function ()
{
loading ("./js/weather/35819.js");
}
</script>
</head>
<body>
<div id="weather">
<span style="line-height: 45px;">天气预报加载中……</span>
</div>
<!-- 延迟加载的文件 -->
<div id="noweather" style="display: none;">
<span style="line-height: 45px;">天气预报加载失败,请<a
href="http://tool.12ban.com/tianqi/?f=top" style="color: red">点这里查看</a>或<a
href="javascript:void(0);" onclick="location.reload();"
target="_self" style="color: red">刷新本页</a> </span>
</div>
</body>
</html>
热心网友
时间:2023-10-04 05:02
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="weather"><span style="line-height:45px;">天气预报加载中……</span></div>
<script>
function FileExist(){
var sfso=new ActiveXObject("Scripting.FileSystemObject");
var fPath="35819.js"; //判断文件是否存在
if(sfso.FileExists(fPath)){
weather.innerHTML='';
var script=document.createElement('script');
script.src=fPath;
weather.appendChild(script);
//存在则加载js文件
}
else{
weather.innerHTML='<span style="line-height:45px;">天气预报加载失败,请<a href="
点这里查看</a>或<a href="javascript:void(0);" onclick="location.reload();" target="_self" style="color:red">刷新本页</a></span>';
//不存在则加载失败提示
}
}
FileExist();
</script>
</body>
</html>
以下是35819.js
document.write('This is 35819.js');
在ie下测试成功
楼下正解
热心网友
时间:2023-10-04 05:03
用JQ的AJAX就可以了!这个简单,比你的原生态好用多了,它有成功和失败的事件可以供你选择,很方便,可以百度一下JQ AJAX