[英]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.