当前位置: 首页 > 图文教程 > 网络编程 > JSP > 用Javascript实现Agent(网页精灵)(1)

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

JSP 中的 用Javascript实现Agent(网页精灵)(1)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 100 ::
收藏到网摘: n/a

一直觉得Agent是用来做网站导航和帮助的不错选择,可惜MS Agent只有IE支持,而且好像加载速度不是很快,分析了一下觉得用Javascript绝对可以做到,以前对Javascript都是抄来抄去,趁此机会也彻底学一学。OK,Let's Begin

Javascript既然是基于对象的语言,那么大体上我们也可以按照OO的思路来设计自己的Agent

我们希望客户端简单调用一下就OK了,应当是如下的形式。

<script src="agent.js"></script>

<script language="JavaScript">
<!--
var agent = new Agent();
agent.run();
//-->
</script>

那么首先我们就来创建一个agent.js,js创建一个对象很简单只需要提供一个构造函数就OK了

function Agent()
{
 this.imgAgent = "images/agent.gif";
}

那么,接下来的任务就是让这个对象拥有一个run方法,这个方法应当仅仅就是输出最后拼凑好的html,我们先简单实现一下。

Agent.prototype.run=function()
{
 var agentHtml = "<img src="+this.imgAgent;
 agentHtml += " id=\"agent1\"";
 agentHtml += " style=\"position:absolute;left:50;top:50;cursor:move\"";
 agentHtml += " onselectstart=\"return false\"";
 agentHtml += " onmousedown=\"mousedown(this)\"";
 agentHtml += " onmouseup=\"mouseup()\"";
 agentHtml += " onmousemove=\"mousemove()\"";
 agentHtml += ">";
 return document.write(agentHtml);
}

从上面代码可以看出,我们实现的第一个精灵效果就是可以拖动精灵到界面任意一个地方,只需实现一下onmousedown,onmousemove,onmousemove三个事件即可

var currentMoveObj = null;        //当前拖动对象

this.setImage=function(img)
{
 imgAgent = img;
}
function dblclick(obj)
{
 obj.src= "images/chaosai.gif";
}
function mousedown(obj)
{
 currentMoveObj = obj;               //当对象被按下时,记录该对象
 //查了一下资料setCapture的意思是:
 //捕捉触发事件时的焦点对象并使鼠标焦点始终绑定该对象。
 //很关键不然灵敏度会很低
 currentMoveObj.setCapture()
 relLeft = event.x - currentMoveObj.style.pixelLeft;
 relTop = event.y - currentMoveObj.style.pixelTop;
}
function mouseup()
{
 if(currentMoveObj!=null)
 {
  currentMoveObj.releaseCapture();
  currentMoveObj=null;
 }
}
function mousemove()
{
 if(currentMoveObj != null)
 {
  currentMoveObj.style.pixelLeft=event.x-relLeft;
  currentMoveObj.style.pixelTop=event.y-relTop;
 }
}