[英]Javascript! IE document ready state
我不是大 javascript 大师,所以如果有人可以帮助我,我会很高兴。 好的,所以我想做的是在准备文件时显示文件下载的简单指示器。 现在一切都在 Firefox 中完美运行,但不是在 IE 加载器中将由 php 创建,下载来自 iframe。 现在正如您从下面的代码中看到的那样,我检查了准备好的文档 state 并且由于某种原因它对 IE 保持交互并且永远不会完成,但在 Firefox 中可以很好地完成。
if($_POST['download'])
{
echo '<iframe id="frame" src ="download.php?data='.$_POST['download'].'" width="0" height="0" frameborder="0" scrolling="no"></iframe>';
echo '<div id="loader">';
echo '<div id="loading-container"><p id="loading-content">Please wait while we prepare your files<img id="loading-graphic" width="220" height="19" src="images/indicator.gif" /></p></div>';
echo '</div>';
echo '<script type="text/javascript">
var wooIntervalId = 0;
var i = 0;
function startInterval()
{
wooIntervalId = setInterval(doSomething, 1000);
}
function doSomething()
{
document.getElementById("loading-content").innerHTML = document.readyState + i;
i++;
}
function remove_elem()
{
element = document.getElementById("loader");
element.parentNode.removeChild(element);
}
startInterval();
</script>';
}
我真的很喜欢在 firefox 中删除此类指示器的解决方案,它会在显示保存文件对话框后立即删除指示器,但在 IE 中它不仅可以工作。
window.onload = function()
{
element = document.getElementById("loader");
element.parentNode.removeChild(element);
}
有什么建议吗?
好的,适用于 IE 和 Firefox 的脚本。 基本上我需要检查 iframe 准备好 state ,它在 Firefox 中变成“NaN”,首先在 IE 中“加载”并在完成时“交互”(IE)所以我创建了值为“正在加载”的startingState,以便在两个浏览器中工作它导致 IE将其 state 更改两次,Firefox 仅更改一次。 在 Chrome 和 Opera 中 iframe 准备好 state 变成“未定义”并且只会执行一次,所以如果有人修复它也会很棒。
<script type="text/javascript">
var startingState = "loading";
var wooIntervalId = 0;
function startInterval()
{
wooIntervalId = setInterval(doSomething, 1000);
}
function doSomething()
{
var doc=document.getElementById("frame");
if(startingState != doc.readyState)
{
remove_elem();
clearInterval(wooIntervalId);
}
}
function remove_elem()
{
element = document.getElementById("loader");
element.parentNode.removeChild(element);
}
startInterval();
</script>';
更新:
使用消息传递 - MSDN - MDN - 将跨域/跨框架从您服务器上的一个页面安全地通信到另一个服务器上的另一个页面
这个怎么样
<?PHP if (isSet($_POST['download'])) { ?>
$url = "download.php?data=".htmlentities($_POST['download']);
<iframe id="frame" src ="<?PHP echo $url; ?>" width="0" height="0" frameborder="0" scrolling="no" onload="remove_elem('loader')"></iframe>
<div id="loader">;
<div id="loading-container"><p id="loading-content">Please wait while we prepare your files<img id="loading-graphic" width="220" height="19" src="images/indicator.gif" /></p></div>
</div>';
<script type="text/javascript">
var wooIntervalId = 0;
var i = 0;
function startInterval() {
wooIntervalId = setInterval(doSomething, 1000);
}
function doSomething() {
document.getElementById("loading-content").innerHTML = document.readyState + i;
i++;
}
function remove_elem(elemId) {
var element = document.getElementById(elemId);
element.parentNode.removeChild(element);
}
startInterval();
</script>
<?PHP } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.