`
wezly
  • 浏览: 480512 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

使用htmlfile隐藏IE浏览器正在打开网页状态的代码

阅读更多

一般情况下,采用长连接,能持续的在客户端显示信息
比如

 
<%@ page language="java" c pageEncoding="utf-8"%>
<%
  out.flush();
  int number = 0;
  while (true) {
    out.println(new java.util.Date()+"
");
    out.flush();
    Thread.sleep(100);
    System.out.print(".");
    if (number++ > 100) {
      break;
    }
  }
%>

这个代码会在客户端连续的显示时间。请注意,浏览器的正在下载是一直在运行中的。这个会让客户感到疑惑,难道还有东西在运行吗?


下面的这个方法,可以解决这个问题

我们先看服务器端代码。
使用了prototype.js

 
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Comet php backend</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
  // KHTML browser don't share javascripts between iframes
  var is_khtml = navigator.appName.match("Konqueror") || navigator.appVersion.match("KHTML");
  if (is_khtml)
  {
    var prototypejs = document.createElement('script');
    prototypejs.setAttribute('type','text/javascript');
    prototypejs.setAttribute('src','../js/prototype.js');
    var head = document.getElementsByTagName('head');
    head[0].appendChild(prototypejs);
  }
  // load the comet object
  var comet = window.parent.comet;
</script>
<%
  out.flush();
  int number = 0;
  while (true) {
    // 这里生成了调用js的代码
    out.println("<script type='text/javascript'>comet.printServerTime('" + new java.util.Date() + "');</script>");
    out.flush();
    Thread.sleep(100);
    // 控制台显示,程序正在运行,正式运行可去掉
    System.out.print(".");
    // 防止程序一直运行,给调试带来麻烦,正式运行可去掉
    if (number++ > 100) {
      break;
    }
  }
%>

关键在客户端代码

 
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Comet demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../js/prototype.js"></script>
</head>
<body>
<div id="content">The server time will be shown here</div>
<script type="text/javascript">
var url= 'testLinkData2.jsp';
var comet = {
  connection   : false,
  iframediv    : false,
  
  initialize: function() {
    if (navigator.appVersion.indexOf("MSIE") != -1) {
    
      // For IE browsers
      comet.connection = new ActiveXObject("htmlfile");
      comet.connection.open();
      comet.connection.write("<html>");
      comet.connection.write("<script>document.domain = '"+document.domain+"'");
      comet.connection.write("</html>");
      comet.connection.close();
      comet.iframediv = comet.connection.createElement("div");
      comet.connection.appendChild(comet.iframediv);
      comet.connection.parentWindow.comet = comet;
      comet.iframediv.innerHTML = "<iframe id='comet_iframe' src='./"+url+"'></iframe>";
    } else if (navigator.appVersion.indexOf("KHTML") != -1) {
      // for KHTML browsers
      comet.connection = document.createElement('iframe');
      comet.connection.setAttribute('id',     'comet_iframe');
      comet.connection.setAttribute('src',    './'+url);
      with (comet.connection.style) {
        position   = "absolute";
        left       = top   = "-100px";
        height     = width = "1px";
        visibility = "hidden";
      }
      document.body.appendChild(comet.connection);
    } else {
    
      // For other browser (Firefox...)
      comet.connection = document.createElement('iframe');
      comet.connection.setAttribute('id',     'comet_iframe');
      with (comet.connection.style) {
        left       = top   = "-100px";
        height     = width = "1px";
        visibility = "hidden";
        display    = 'none';
      }
      comet.iframediv = document.createElement('iframe');
      comet.iframediv.setAttribute('src', './'+url);
      comet.connection.appendChild(comet.iframediv);
      document.body.appendChild(comet.connection);
    }
  },
  // this function will be called from backend.php  
  printServerTime: function (time) {
    $('content').innerHTML = time;
  },
  onUnload: function() {
    if (comet.connection) {
      comet.connection = false; // release the iframe to prevent problems with IE when reloading the page
    }
  }
}
Event.observe(window, "load",   comet.initialize);
Event.observe(window, "unload", comet.onUnload);
</script>
</body>
</html>

请注意其中的
comet.connection = new ActiveXObject("htmlfile");
部分,就是这段代码生成了一个可以在IE浏览器下面,不再显示正在下载状态。据说这个是google的聪明人发现并应用于google的gtalk和gmail


最后,我们来看使用Ajax的查询方式,
服务器端在没有数据的情况下会阻塞,直到有数据。
服务器端

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
  out.flush();
  int number = 0;
  while (true) {
    // 这里可以插入检测的代码,我后面直接休眠100毫秒代替了
    out.println("{'timestamp':'"+System.currentTimeMillis()+"','msg':'"+ new java.util.Date() + "'}");
    out.flush();
    
    Thread.sleep(100);
    System.out.print(".");
    if (number++ > 1) {
      break;
    }
    break;
  }
%>

看关键的客户端

 
 
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Comet demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../js/prototype.js"></script>
</head>
<body>
<div id="content">内容</div>
<script type="text/javascript">
var Comet = Class.create();
Comet.prototype = {
  timestamp: 0,
  url: './testLinkData3.jsp',
  noerror: true,
  initialize: function() { },
  connect: function()
  {
    this.ajax = new Ajax.Request(this.url, {
      method: 'get',
      parameters: { 'timestamp' : this.timestamp },
      onSuccess: function(transport) {
        // handle the server response
        var response = transport.responseText.evalJSON();
        this.comet.timestamp = response['timestamp'];
        this.comet.handleResponse(response);
        this.comet.noerror = true;
      },
      onComplete: function(transport) {
        // send a new ajax request when this request is finished
        if (!this.comet.noerror)
          // if a connection problem occurs, try to reconnect each 5 seconds
          setTimeout(function(){ comet.connect() }, 5000); 
        else
          this.comet.connect();
        this.comet.noerror = false;
      }
    });
    this.ajax.comet = this;
  },
  disconnect: function()
  {
  },
  handleResponse: function(response)
  {
    $('content').innerHTML += '<div>' + response['msg'] + '</div>';
  },
  doRequest: function(request)
  {
    new Ajax.Request(this.url, {
      method: 'get',
      parameters: { 'msg' : request }
    });
  }
}
var comet = new Comet();
comet.connect();
</script>
</body>
</html>
 
 
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    HTML中文件上传时使用的input type=”file”元素的样式自定义

    在某些情况下,我们可能需要进一步隐藏`&lt;input type="file"&gt;`元素本身,比如在IE浏览器中,使用`left`属性定位会导致输入框可见。为了避免这种情况,我们可以设置`input`的`opacity`为0,并使用`-ms-filter`滤镜在IE...

    网页保护方法大全.pdf

    `disdrag()`函数的目的是在IE浏览器中禁止页面拖拽,而`window.location="";`则会在页面尝试加载时重定向到空地址,实现某些安全保护。 9. 禁止链接跳转 在脚本中通过条件判断`if(window.location.protocol.indexOf...

    一些初学都常用的ASP代码

    禁止IE浏览器滚动条显示 ```html ``` 这段HTML代码用于在Internet Explorer浏览器中隐藏页面的垂直滚动条,从而实现全屏效果。 ### 5. 弹出新窗口 ```html &lt;!-- &lt;!-- 打开新窗口 --&gt; window.open('...

    javascript 使用技巧

    此方法仅在IE浏览器中有效,其他浏览器可能不支持`fileSize`属性。 #### 4. 动态删除DOM元素 使用`removeNode()`方法可以动态删除DOM元素,例如: ```javascript var img1 = document.getElementById('myImage');...

    js 实用小例子

    - 示例代码演示了如何实现文本滚动动画、数字计时器以及简单显示和隐藏元素的动画。 - 文本滚动动画通过逐字符显示字符串来实现。 - 数字计时器使用`setInterval`每秒更新计数器。 - 显示/隐藏元素动画通过切换...

    400个DreamWeaver插件

    mxp/想自己定义IE浏览器的滚动条吗?用这个插件吧,IE5.5以上支持 mxp/插入上标、下标 mxp/在代码编辑状态下,点击右键,选择Insert JS Function Call,会在当前位置插入定义过的函数名 mxp/使得M18 - Netscape 6...

    js使用小技巧

    注意,此方法仅适用于旧版IE浏览器。 26. **同时执行两个事件**: 通过在函数声明中逗号分隔,可以同时注册两个事件处理函数: ```javascript function document.onclick, document.onkeydown() { window....

    Comet:基于_HTTP_长连接的“服务器推”技术

    - 缺点:兼容性较差,主要依赖于IE浏览器的特性,不适用于其他浏览器环境。 #### 开发Comet应用的注意事项 在开发基于Comet的应用时,需要注意以下几个关键点: 1. **跨域问题**:确保服务器端能够正确处理跨域...

    LotusDomino学习笔记.doc

    - **代码示例**: 展示如何使用JavaScript打开指定尺寸的弹出窗口。 #### 75. javascript的容错脚本 - **容错脚本**: 展示如何使用JavaScript处理错误和异常情况。 #### 76. Web search with JavaScript - **搜索...

    UniGUI集合说明--追月无名.pdf

    - **使用条件注释**:针对IE浏览器编写特定的CSS样式。 - **升级浏览器**:建议用户使用更现代的浏览器,如Chrome、Firefox等。 #### 3. 在浏览器端不能使用 ShowMessage() `ShowMessage()` 是UniGUI中用于弹出...

    UniGUI集合说明.doc

    在IE浏览器中,UniGUI控件的字体可能会变小或模糊,这可能是因为浏览器的兼容性问题或CSS样式设置不正确。可以通过调整控件的CSS样式或采用跨浏览器的字体设置来解决。 3. **ShowMessage()在浏览器端** 浏览器...

    delphi 开发经验技巧宝典源码

    0016 使用快捷键打开对象观察器中的“...”按钮 11 1.5 其他相关应用技巧 11 0017 安装合适的Delphi版本 11 0018 熟练掌握Delphi中的菜单项 12 0019 构成Delphi项目的主要文件 12 0020 如何在程序中调用...

    delphi 开发经验技巧宝典源码06

    0016 使用快捷键打开对象观察器中的“...”按钮 11 1.5 其他相关应用技巧 11 0017 安装合适的Delphi版本 11 0018 熟练掌握Delphi中的菜单项 12 0019 构成Delphi项目的主要文件 12 0020 如何在程序中调用...

    UniGUI集合说明

    - **使用Web标准兼容模式**:确保IE浏览器以Web标准兼容模式运行,避免使用旧版渲染引擎。 - **调整DPI设置**:对于高分辨率显示器,适当调整DPI设置也可能有助于改善显示效果。 #### 3. 在浏览器端不能使用`...

    大名鼎鼎SWFUpload- Flash+JS 上传

    SWFUpload使用一个隐藏的Flash影片来控制文件的选择和上传。JavaScript用来激活文件选择对话框。 此文件选择对话框是可以设置允许用户选择一个单独的文件或者是多个文件。 选择的的文件类型也是可以被限制的,因此...

    网管教程 从入门到精通软件篇.txt

    不扫描驱动器看是否有坏区域,因此只应对以前格式化过的驱动器使用该参数。  /fs:file-system  指定要使用的文件系统:FAT、FAT32 或 NTFS 。如果未指定文件系统,将使用现有的文件系统格式。  Map  显示...

Global site tag (gtag.js) - Google Analytics