2012-08-28 110 views
2

div“intro”的JavaScript最后加载。加载网页首先加载bg图像并加载java脚本需要很长时间才能加载。有没有一种方法可以在“intro”div中显示“加载请等待”消息,直到它完全加载。我只是希望介绍首先加载。JavaScript花太长时间加载

Javascript代码:

var tl = new Array(
    "=======================", 
    " Welcome user, ", 
    " ###########################################" 
); 
var speed = 50; 
var index = 0; 
text_pos = 0; 
var str_length = tl[0].length; 
var contents, row; 

function type_text() { 
    contents = ''; 
    row = Math.max(0, index - 20); 
    while (row < index) 
    contents += tl[row++] + '\r\n'; 
    document.forms[0].elements[0].value = contents + tl[index].substring(0, text_pos) + "_"; 
    if (text_pos++ == str_length) { 
     text_pos = 0; 
     index++; 
     if (index != tl.length) { 
      str_length = tl[index].length; 
      setTimeout("type_text()", 500); 
     } 
    } 
    else setTimeout("type_text()", speed); 
} 

这是脚本,它基本上由键入字母在格“介绍”一文区信。问题在于,当整个页面加载时,它最后加载。它在15秒左右后开始打印文本。

+0

你的脚本如何初始化?你正在利用onpageload事件或domready? – WTK

+0

'网页首先加载bg图像' - 图像有多大? –

+0

我正在使用onload和bg图像是400KB – Bhavyanshu

回答

2

您可以在document上听到的“domready”事件,但似乎是not cross-browser

如:Mozilla的

document.addEventListener("DOMContentLoaded", methodName, false) 

一个更好的选择是使用jQuery的.ready()事件。他们处理所有跨浏览器的实现。

如:

$(document).ready(function(){ 
    //execute code here 
}); 

//Shorthand 
$(function(){ 
//... 
}); 

更多关于domready看到这个related question

+0

感谢这工作! – Bhavyanshu

0

您可以通过包装在div标签中保存加载图像,东西这种效果的内容,然后另一个DIV使用这个jQuery:

$(document).ready(function() { 
    $('#loading').show(); 

    $('#divShowMeLater').load(function() { 
     $('#loading').hide(); 
     $('#divShowMeLater').show(); 
    }); 
}) 

假设divShowMeLater是包含所有内容的DIV正在加载。该标记看起来类同此:

<div id="divShowMeLater" style="margin-left:auto;margin-right:auto;text-align:center;" > 
    <div id="loading">Page loading... 
     <img src="images/ajax-loader.gif" alt="loading page..." /> 
    </div> 
</div> 
+0

即使在div加载的内容之后,它也不会隐藏“Page loading ...”消息。 – Bhavyanshu

+0

你的div被称为'id = loading',这是我的例子,所以你必须使它适合你的需求。 ready函数最初显示div'$('#loading')。show();',然后在其他div加载后隐藏它。 – JonH

0

负载与空介绍DIV一个页面,用“请耐心等待”,然后触发一个Ajax请求加载页面的其余部分运行脚本和更新页面从Ajax请求

0

的onComplete事件使用jQuery

$(document).ready(function() { 
     // update div here 
    }); 

http://api.jquery.com/ready/

或者你可以做到这一点与

window.onload= (function() { 
    // update div here   
}; 
+2

jQuery的'document.ready'和本地'window.onload'不一样。 – dfsq