window.onload = initAll;
var xhr = false;
var xPos,yPos;
function initAll() {
var allLinks = document.getElementsByTagName("a");
for(var i=0;i<allLinks.length;i++) {
allLinks[i].onmouseover = showPreview;
}}
function showPreview(evt){
getPreview(evt);
return false;
}
function getPreview(evt){
if(evt) {
var url = evt.target;
} else {
evt = window.event;
var url = evt.srcElement;
} //URL保存在evt.target或window.event.srcElement中
xPos = evt.clientX;
yPos = evt.clientY; //获取当前鼠标的XY坐标
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if(window.ActiveXObject) {
try{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {}
} //创建xhr对象
if(xhr) {
xhr.onreadystatechange = showContents;
xhr.open("GET",url,true);
xhr.send(null);
}
else {
alert("Sorry,but I couldn't create an XMLHttpRequest");
}} //使用xhr必做的三件事情,与服务器通信,异步取得所需文件
function showContents() {
var prevWin = document.getElementById("previewWin");
if(xhr.readyState == 4) {
prevWin.innerHTML = (xhr.status == 200)?xhr.responseText:"There was a problem with the request " + xhr.status;
prevWin.style.top = parseInt(yPos)+2+"px";
prevWin.style.left = parseInt(xPos)+2+"px";
prevWin.style.visibility = "visible";
prevWin.onmouseout = hidePreview;
}} //将xhr.responseText放进innerHTML会告诉浏览器当显示预览窗口时,它应当将这里的内容解释为HTML。
function hidePreview(){
document.getElementById("previewWin").style.visibility = "hidden";
}
链接预览
最新推荐文章于 2023-12-28 18:46:00 发布