loading.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var xmlHttp;
  2. var url="http://www.qq.com";
  3. function createXMLHttpRequest() { //创建一个xmlHttpRequest对象
  4. if (window.ActiveXObject) {
  5. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  6. }
  7. else if (window.XMLHttpRequest) {
  8. xmlHttp = new XMLHttpRequest();
  9. }
  10. }
  11. function loading(){
  12. createXMLHttpRequest();
  13. xmlHttp.onreadystatechange = handleStateChange; //请求状态改变事件触发handleStateChange功能
  14. xmlHttp.open("GET",url); //采用get方法提交数据
  15. xmlHttp.send(null);
  16. }
  17. function handleStateChange(){
  18. var loadcontent = document.createElement("div");
  19. loadcontent.style.width=document.body.clientWidth;
  20. loadcontent.style.height=document.documentElement.clientHeight;
  21. loadcontent.className="loading";
  22. document.body.appendChild(loadcontent);
  23. if(xmlHttp.readystate == 4){ //表示请求状态 4为完成
  24. if(xmlHttp.status == 200){ //http状态指示码,200表示ok
  25. loadcontent.style.display="none";
  26. }
  27. }
  28. else loadcontent.style.display="block";//若响应未完成的话,则显示loading..
  29. }
  30. if (document.all){
  31. window.attachEvent('onload',loading)//IE中,document.all在这里是起到判断是否是ie的功能
  32. }
  33. else{
  34. window.addEventListener('load',loading,false);//firefox
  35. }