优化Java Applet的加载过程
大家知道,在java applet还没有全部加载的时候,IE或Netscape会在applet对应的区域显示一块灰色的区域。那,您有没有想过用其他的方式代替它呢?
本文介绍了如何利用javascript 实现在加载java applet时候显示一段消息,比如请稍候之类的话。该段代码可以在IE和Netscape运行。
下面的代码就可以解决这个问题。
<script language="javascript"> <BR><!-- Hide script from old browsers <BR> function init(){ <BR> // Microsoft Internet Explorer <BR> if (document.all){ <BR> document.all.loading.style.visibility="hidden"; <BR> document.all.myapplet.style.visibility="visible"; <BR> document.applets[0].repaint(); <BR> } <BR> // Netscape Navigator <BR> else{ <BR> document.loading.visibility="hide"; <BR> document.myapplet.visibility="visible"; <BR> } <BR> } <BR>// --> <BR></script> <BR><BR><BODY onLoad="init()"> <BR><div id="loading" style="position:absolute;left:150;top:10;"> <BR>Please wait while Java applet loads... <BR></div> <BR><div id="myapplet" style="position:absolute;left:150;top:10;visibility:hidden"> <BR><applet code="charts.charts.class" archive="charts.jar" width="600" height="400" codebase="" VIEWASTEXT> <BR><Param name="xValue" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, <BR>23,24,25,26,27,28,29,30"> <BR><Param name="yValue" value="22,21,19,23,18,24,28,30,27,22,20,18,16,18,19,16,17,14,12, <BR>13,16,18,19,20,16,17,13,10,14,19,25"> <BR></applet> <BR></div> <BR></BODY> <BR><BR>解释: <BR> 首先,让我们来看看我们真正想做的是什么. 其实我们只不过希望把下载java程序过程中的不雅观的灰块去掉,以一些消息如正在载入,请稍候之类的画面盖住原来的灰色区域。而一旦java加载完毕,马上把该区域恢复为applet可见。 <BR> 上面的javascript代码就是实现了这个目的的一个实例。 <BR> 下面,让我来逐条解释一下我们是如何实现的。 <BR> 首先请看以下部分。 <BR><BR><BR><BR><div id="loading" style="position:absolute;left:150;top:10;"> <BR>Please wait while Java applet loads... <BR></div> <BR><div id="myapplet" style="position:absolute;left:150;top:10;visibility:hidden"> <BR><applet code="charts.charts.class" archive="charts.jar" width="600" height="400" codebase="" VIEWASTEXT> <BR><Param name="xValue" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, <BR>23,24,25,26,27,28,29,30"> <BR><Param name="yValue" value="22,21,19,23,18,24,28,30,27,22,20,18,16,18,19,16,17,14,12, <BR>13,16,18,19,20,16,17,13,10,14,19,25"> <BR></applet> <BR></div> <BR> 正如您所见到的,我用了2个层,第一个层(id为loading)可以写上一些提示消息。而第2个层(id为myapplet)在一开始是不可见的。当applet下载好了,马上把第2个层显示出来,并把第一个层设为不可见。因此,只要这2个层大小,位置必须完全一致。就可以实现我们的目标。 <BR> 现在我们只差最后一个问题需要解决:如何知道applet已经完全下载了呢?在IE和netscape中都有提供document.all这个属性,当应页面的全部内容(包括applets,图片,声音等)已经下载后该属性为真。好,这样,我们只需监测document.all是否为真,如果是,那就可以把消息所在的层设为不可见,而applet所在的层设为可见,并刷新applet的显示(因为先前applet的显示被隐藏了),否则则相反处理。 <BR><BR><BR><BR>这是处理后的代码: <BR><script language="javascript"> <BR> function init() <BR> { <BR> if (document.all) <BR> { <BR> document.all.loading.style.visibility="hidden"; <BR> document.all.myapplet.style.visibility="visible"; <BR> document.applets[0].repaint(); <BR> } <BR> else <BR> { <BR> document.loading.visibility="hide"; <BR> document.myapplet.visibility="visible"; <BR> } <BR> } <BR></script> <BR> 大功告成。其实,用这个办法同样适用于用含有图片盖住applet直到applet开始运行为止。如何处理,我想聪明的你一定已经知道答案了。 <BR><br><br> </div> </div> </DIV> <!-- 文章内容结束 --> <!-- 拷贝连接开始 --> <DIV class=trclear></DIV> <DIV> <script type="text/javascript"><!-- google_ad_client = "pub-7396688374477371"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "000000"; google_color_text = "000000"; google_color_url = "666666"; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <INPUT title=点击复制标题和地址到剪贴版,直接在POPO/MSN上粘贴即可. onclick=copyToClipBoard() type=button value=点击复制本网址,发给POPO/QQ/MSN好友共享 name=B3></DIV> <!-- 拷贝连接结束 --> <DIV class=trclear></DIV> <DIV class=trclear></DIV> <!-- 内容调用开始 --> <iframe src="../../../../pagepic/newspic_c_id_92_pageno_3_s_id_258.html" width="778" height="90" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"> </iframe> <!-- 内容调用结束 --> <DIV class=trclear></DIV> <DIV class=trclear></DIV> <!-- 内容功能、搜索开始 --> <DIV class="adText"> <DIV class=col> <UL> <LI>发布人:<a href='../../../../user_view_username_java.html' alt='查看 java 的详细资料' target=_blank>java</a> </LI> <LI><a href='javascript:;' onclick="javascript:open_win('/commend.asp?nsort=java%CE%C4%D5%C2%A3%A8Applet%2FSwing%A3%A9%2D+%D3%C5%BB%AFJava+Applet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&topic=%D3%C5%BB%AFJavaApplet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&url=article%5Fview%2Easp%3Fid%3D6727','commend',500,380,'no');">→ 推荐给我的好友</a> <a href='javascript:;' onclick="javascript:open_win('/err.asp?nsort=java%CE%C4%D5%C2%A3%A8Applet%2FSwing%A3%A9%2D+%D3%C5%BB%AFJava+Applet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&topic=%D3%C5%BB%AFJavaApplet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&eid=6727&n_sort=art&url=err%2Easp%3Fid%3D6727','commend',500,325,'no');">→ 报告错误链接</a> </LI> </UL></DIV> <DIV class=colt> <UL> <LI>上篇文章:<a href=/html/2006/article/info6/a_bc0e4d8215f9452f.htm>JSP漏洞大观</a> </LI> <LI>下篇文章:<a href=/html/2006/article/info6/a_6fe6d0f5084dd1ea.htm>Java2下Applet数字签名</a> </LI> </UL></DIV> <DIV class=cold> <UL> <LI><a href='../../../../article_print_id_6727.html' target=_blank>〖文章打印〗</a> </LI> <LI><a href="javascript:self.close()">〖关闭窗口〗</a> </LI> </UL></DIV> <DIV class=cold> <UL> <LI><a href="../../../../reviwepage_id_6727.html" target="_blank">发表评论</a> </LI> <LI><a href="../../../../reviwepage_id_6727.html" target="_blank">查看评论</a> </LI> </UL></DIV> <DIV class=trclear></DIV> <DIV class=colp> <UL> <LI><IMG src="../../../../images/dc/icon_google.gif" align=absMiddle border=0><A onclick='window.open("http://www.google.com/search?hl=zh-CN&ie=GBK&q=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> <LI><IMG src="../../../../images/dc/icon_sogou.gif" align=absMiddle border=0><A onclick='window.open("http://www.sogou.com/web?query=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> <LI><IMG src="../../../../images/dc/icon_epku.gif" align=absMiddle border=0> <A onclick='window.open("http://e.pku.edu.cn/cgi-bin/allsearch?cdtype=GB&word=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> </UL></DIV> <DIV class=colp> <UL> <LI><IMG src="../../../../images/dc/icon_baidu.gif" align=absMiddle border=0> <A onclick='window.open("http://www.baidu.com/baidu?word=优化JavaApplet的加载过程&tn=eunice_pg")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A></a> </LI> <LI><IMG src="../../../../images/dc/icon_yisou.gif" align=absMiddle border=0><A onclick='window.open("http://search.cn.yahoo.com/search?p=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> <LI><IMG src="../../../../images/dc/icon_zhongsou.gif" align=absMiddle border=0><A onclick='window.open("http://page.zhongsou.com/zspage.cgi?word=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> </UL></DIV> </DIV> <!-- 内容功能、搜索开始 --> <DIV class=clear></DIV> <!-- 页脚开始 --> <div class="foot"> <div class="tr"> <a href="../../../../help.html">关于我们</a> - <a href="../../../../gbook.html">网站留言</a> - <a href="../../../../links.html">友情链接</a> - <a href="../../../../online.html">与我在线</a> - <a href="../../../../chat.html">与我聊天</a> - <a href="../../../../get_emoney.html">领取红包</a> - <a href="#top">TOP</a> </div> <br>客户服务中心信箱:<a href="mailto:[email protected]">[email protected]</a> <a href="mailto:[email protected]">[email protected]</a> <a href="../../../../51dibs_map.htm" target="_blank">网站地图</a><br><br> <a href="http://www.miibeian.gov.cn/login.html">粤ICP备05020177号</a> <a href="../../../../sm.htm" target="_blank">声明</a><br><br>合作伙伴:<a href="http://www.szhebe.com" target=_blank>深圳赫柏文化发展有限公司</a> <a href="http://www.jkbox.com" target=_blank>健康百宝箱</a> <a href="http://www.dbfounder.com" target=_blank>东北人创业家园</a> </div> <!-- 页脚结束 --> <SCRIPT language=javascript> function copyToClipBoard(){ var clipBoardContent=''; clipBoardContent+="优化JavaApplet的加载过程 - 爱心种子小博士"; clipBoardContent+='\r\nhttp://www.51dibs.com/html/2006/article/info6/a_f7b71fa5ef36e414.htm'; window.clipboardData.setData("Text",clipBoardContent); //alert("你已复制链接及标题,请粘贴到POPO/MSN等发给好友!"); } </SCRIPT> <script src="http://www.51dibs.com/dbcount/ajiang/mystat_siteid_1.html"></script> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-304444-1"; urchinTracker(); </script> </DIV> </body></html>
大家知道,在java applet还没有全部加载的时候,IE或Netscape会在applet对应的区域显示一块灰色的区域。那,您有没有想过用其他的方式代替它呢?
本文介绍了如何利用javascript 实现在加载java applet时候显示一段消息,比如请稍候之类的话。该段代码可以在IE和Netscape运行。
下面的代码就可以解决这个问题。
<script language="javascript"> <BR><!-- Hide script from old browsers <BR> function init(){ <BR> // Microsoft Internet Explorer <BR> if (document.all){ <BR> document.all.loading.style.visibility="hidden"; <BR> document.all.myapplet.style.visibility="visible"; <BR> document.applets[0].repaint(); <BR> } <BR> // Netscape Navigator <BR> else{ <BR> document.loading.visibility="hide"; <BR> document.myapplet.visibility="visible"; <BR> } <BR> } <BR>// --> <BR></script> <BR><BR><BODY onLoad="init()"> <BR><div id="loading" style="position:absolute;left:150;top:10;"> <BR>Please wait while Java applet loads... <BR></div> <BR><div id="myapplet" style="position:absolute;left:150;top:10;visibility:hidden"> <BR><applet code="charts.charts.class" archive="charts.jar" width="600" height="400" codebase="" VIEWASTEXT> <BR><Param name="xValue" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, <BR>23,24,25,26,27,28,29,30"> <BR><Param name="yValue" value="22,21,19,23,18,24,28,30,27,22,20,18,16,18,19,16,17,14,12, <BR>13,16,18,19,20,16,17,13,10,14,19,25"> <BR></applet> <BR></div> <BR></BODY> <BR><BR>解释: <BR> 首先,让我们来看看我们真正想做的是什么. 其实我们只不过希望把下载java程序过程中的不雅观的灰块去掉,以一些消息如正在载入,请稍候之类的画面盖住原来的灰色区域。而一旦java加载完毕,马上把该区域恢复为applet可见。 <BR> 上面的javascript代码就是实现了这个目的的一个实例。 <BR> 下面,让我来逐条解释一下我们是如何实现的。 <BR> 首先请看以下部分。 <BR><BR><BR><BR><div id="loading" style="position:absolute;left:150;top:10;"> <BR>Please wait while Java applet loads... <BR></div> <BR><div id="myapplet" style="position:absolute;left:150;top:10;visibility:hidden"> <BR><applet code="charts.charts.class" archive="charts.jar" width="600" height="400" codebase="" VIEWASTEXT> <BR><Param name="xValue" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, <BR>23,24,25,26,27,28,29,30"> <BR><Param name="yValue" value="22,21,19,23,18,24,28,30,27,22,20,18,16,18,19,16,17,14,12, <BR>13,16,18,19,20,16,17,13,10,14,19,25"> <BR></applet> <BR></div> <BR> 正如您所见到的,我用了2个层,第一个层(id为loading)可以写上一些提示消息。而第2个层(id为myapplet)在一开始是不可见的。当applet下载好了,马上把第2个层显示出来,并把第一个层设为不可见。因此,只要这2个层大小,位置必须完全一致。就可以实现我们的目标。 <BR> 现在我们只差最后一个问题需要解决:如何知道applet已经完全下载了呢?在IE和netscape中都有提供document.all这个属性,当应页面的全部内容(包括applets,图片,声音等)已经下载后该属性为真。好,这样,我们只需监测document.all是否为真,如果是,那就可以把消息所在的层设为不可见,而applet所在的层设为可见,并刷新applet的显示(因为先前applet的显示被隐藏了),否则则相反处理。 <BR><BR><BR><BR>这是处理后的代码: <BR><script language="javascript"> <BR> function init() <BR> { <BR> if (document.all) <BR> { <BR> document.all.loading.style.visibility="hidden"; <BR> document.all.myapplet.style.visibility="visible"; <BR> document.applets[0].repaint(); <BR> } <BR> else <BR> { <BR> document.loading.visibility="hide"; <BR> document.myapplet.visibility="visible"; <BR> } <BR> } <BR></script> <BR> 大功告成。其实,用这个办法同样适用于用含有图片盖住applet直到applet开始运行为止。如何处理,我想聪明的你一定已经知道答案了。 <BR><br><br> </div> </div> </DIV> <!-- 文章内容结束 --> <!-- 拷贝连接开始 --> <DIV class=trclear></DIV> <DIV> <script type="text/javascript"><!-- google_ad_client = "pub-7396688374477371"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "000000"; google_color_text = "000000"; google_color_url = "666666"; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <INPUT title=点击复制标题和地址到剪贴版,直接在POPO/MSN上粘贴即可. onclick=copyToClipBoard() type=button value=点击复制本网址,发给POPO/QQ/MSN好友共享 name=B3></DIV> <!-- 拷贝连接结束 --> <DIV class=trclear></DIV> <DIV class=trclear></DIV> <!-- 内容调用开始 --> <iframe src="../../../../pagepic/newspic_c_id_92_pageno_3_s_id_258.html" width="778" height="90" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"> </iframe> <!-- 内容调用结束 --> <DIV class=trclear></DIV> <DIV class=trclear></DIV> <!-- 内容功能、搜索开始 --> <DIV class="adText"> <DIV class=col> <UL> <LI>发布人:<a href='../../../../user_view_username_java.html' alt='查看 java 的详细资料' target=_blank>java</a> </LI> <LI><a href='javascript:;' onclick="javascript:open_win('/commend.asp?nsort=java%CE%C4%D5%C2%A3%A8Applet%2FSwing%A3%A9%2D+%D3%C5%BB%AFJava+Applet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&topic=%D3%C5%BB%AFJavaApplet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&url=article%5Fview%2Easp%3Fid%3D6727','commend',500,380,'no');">→ 推荐给我的好友</a> <a href='javascript:;' onclick="javascript:open_win('/err.asp?nsort=java%CE%C4%D5%C2%A3%A8Applet%2FSwing%A3%A9%2D+%D3%C5%BB%AFJava+Applet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&topic=%D3%C5%BB%AFJavaApplet%B5%C4%BC%D3%D4%D8%B9%FD%B3%CC&eid=6727&n_sort=art&url=err%2Easp%3Fid%3D6727','commend',500,325,'no');">→ 报告错误链接</a> </LI> </UL></DIV> <DIV class=colt> <UL> <LI>上篇文章:<a href=/html/2006/article/info6/a_bc0e4d8215f9452f.htm>JSP漏洞大观</a> </LI> <LI>下篇文章:<a href=/html/2006/article/info6/a_6fe6d0f5084dd1ea.htm>Java2下Applet数字签名</a> </LI> </UL></DIV> <DIV class=cold> <UL> <LI><a href='../../../../article_print_id_6727.html' target=_blank>〖文章打印〗</a> </LI> <LI><a href="javascript:self.close()">〖关闭窗口〗</a> </LI> </UL></DIV> <DIV class=cold> <UL> <LI><a href="../../../../reviwepage_id_6727.html" target="_blank">发表评论</a> </LI> <LI><a href="../../../../reviwepage_id_6727.html" target="_blank">查看评论</a> </LI> </UL></DIV> <DIV class=trclear></DIV> <DIV class=colp> <UL> <LI><IMG src="../../../../images/dc/icon_google.gif" align=absMiddle border=0><A onclick='window.open("http://www.google.com/search?hl=zh-CN&ie=GBK&q=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> <LI><IMG src="../../../../images/dc/icon_sogou.gif" align=absMiddle border=0><A onclick='window.open("http://www.sogou.com/web?query=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> <LI><IMG src="../../../../images/dc/icon_epku.gif" align=absMiddle border=0> <A onclick='window.open("http://e.pku.edu.cn/cgi-bin/allsearch?cdtype=GB&word=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> </UL></DIV> <DIV class=colp> <UL> <LI><IMG src="../../../../images/dc/icon_baidu.gif" align=absMiddle border=0> <A onclick='window.open("http://www.baidu.com/baidu?word=优化JavaApplet的加载过程&tn=eunice_pg")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A></a> </LI> <LI><IMG src="../../../../images/dc/icon_yisou.gif" align=absMiddle border=0><A onclick='window.open("http://search.cn.yahoo.com/search?p=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> <LI><IMG src="../../../../images/dc/icon_zhongsou.gif" align=absMiddle border=0><A onclick='window.open("http://page.zhongsou.com/zspage.cgi?word=优化JavaApplet的加载过程")' href="javascript:"> 中“优化JavaApplet的加载过程”相关内容</A> </LI> </UL></DIV> </DIV> <!-- 内容功能、搜索开始 --> <DIV class=clear></DIV> <!-- 页脚开始 --> <div class="foot"> <div class="tr"> <a href="../../../../help.html">关于我们</a> - <a href="../../../../gbook.html">网站留言</a> - <a href="../../../../links.html">友情链接</a> - <a href="../../../../online.html">与我在线</a> - <a href="../../../../chat.html">与我聊天</a> - <a href="../../../../get_emoney.html">领取红包</a> - <a href="#top">TOP</a> </div> <br>客户服务中心信箱:<a href="mailto:[email protected]">[email protected]</a> <a href="mailto:[email protected]">[email protected]</a> <a href="../../../../51dibs_map.htm" target="_blank">网站地图</a><br><br> <a href="http://www.miibeian.gov.cn/login.html">粤ICP备05020177号</a> <a href="../../../../sm.htm" target="_blank">声明</a><br><br>合作伙伴:<a href="http://www.szhebe.com" target=_blank>深圳赫柏文化发展有限公司</a> <a href="http://www.jkbox.com" target=_blank>健康百宝箱</a> <a href="http://www.dbfounder.com" target=_blank>东北人创业家园</a> </div> <!-- 页脚结束 --> <SCRIPT language=javascript> function copyToClipBoard(){ var clipBoardContent=''; clipBoardContent+="优化JavaApplet的加载过程 - 爱心种子小博士"; clipBoardContent+='\r\nhttp://www.51dibs.com/html/2006/article/info6/a_f7b71fa5ef36e414.htm'; window.clipboardData.setData("Text",clipBoardContent); //alert("你已复制链接及标题,请粘贴到POPO/MSN等发给好友!"); } </SCRIPT> <script src="http://www.51dibs.com/dbcount/ajiang/mystat_siteid_1.html"></script> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-304444-1"; urchinTracker(); </script> </DIV> </body></html>