Use ajax to preview the link to view the content of the link. ajax Preview
First, let's look at the implementation code.
Html code
<!DOCTYPE html>
This CSS sets the style of the preview pop-up window
#previewWin {background-color: #FF9;width: 400px;height: 100px;font: .8em arial, helvetica, sans-serif;padding: 5px;position: absolute;visibility: hidden;top: 10px;left: 10px;border: 1px #CC0 solid;clip: auto;overflow: hidden;}#previewWin h1, #previewWin h2 {font-size: 1.0em;}
This JavaScript is used for server requests and a pop-up window is displayed.
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 = getPreview;}}function getPreview(evt) {if (evt) {var url = evt.target;}else {evt = window.event;var url = evt.srcElement;}xPos = parseInt(evt.clientX);yPos = parseInt(evt.clientY);if (window.XMLHttpRequest) {xhr = new XMLHttpRequest();}else {if (window.ActiveXObject) {try {xhr = new ActiveXObject("Microsoft.XMLHTTP");}catch (e) { }}}if (xhr) {xhr.onreadystatechange = showContents;xhr.open("GET", url, true);xhr.send(null);}else {alert("Sorry, but I couldn't create an XMLHttpRequest");}}function hidePreview() {document.getElementById("previewWin").style.visibility = "hidden";}function showContents() {var prevWin = document.getElementById("previewWin");if (xhr.readyState == 4) {if (xhr.status == 200) {prevWin.innerHTML = xhr.responseText;}else {prevWin.innerHTML = "There was a problem with the request " + xhr.status;}prevWin.style.top = yPos+2 + "px";prevWin.style.left = xPos+2 + "px";prevWin.style.visibility = "visible";prevWin.onmouseout = hidePreview;}}
Analysis:
1.
var allLinks = document.getElementsByTagName("a");for (var i=0; i< allLinks.length;i++) {allLinks[i].onmouseover = getPreview;}
This is the content of the initAll () function. it traverses all links on the page and adds an onmouseover event to each link.
Processing Program. This event handler will read the target page (which you will see below) and display a preview to (possibly) visitors.
2.
if (evt) {var url = evt.target;}else {evt = window.event;var url = evt.srcElement;}xPos = parseInt(evt.clientX);yPos = parseInt(evt.clientY);
In getPreview (), you must first find out the file to be read, which requires you to view the properties of the event. Based on the visitor's
If the page views are different, the URL is stored in evt.tar get or window. event. srcElement. After obtaining the URL, you can obtain
The x and y positions of the mouse are for future use.
3.
function hidePreview() {document.getElementById ("previewWin").style.visibility = "hidden";}
If you want to display the preview, you need to hide it again, right? The hidePreview () function is used to preview the visibility of the window.
Reset to hide.
4.
Var prevWin = document. getElementById ("previewWin ");
If (xhr. readyState = 4 ){
After reading the File Using Ajax, The showContents () function is now available. We store the previewWin element in prevWin
For later use. When xhr. readyState is 4, preview is displayed.
5.
if (xhr.status == 200) {prevWin.innerHTML = xhr.responseText;}else {prevWin.innerHTML = "There was a problem with the request " + xhr.status;}prevWin.style.top = yPos+2 + "px";prevWin.style.left = xPos+2 +"px";prevWin.style.visibility ="visible";prevWin.onmouseout = hidePreview;
If everything works, xhr. status is 200, and the data we want to put in prevWin. innerHTML has been saved
In xhr. responseText. If a problem occurs, put an error message in prevWin. innerHTML.
After that, you need to find out where to display the preview window, that is, the current mouse x and y coordinates. This window is a bullet
Out of the window, so place it in the position of the current mouse that triggers the call down and a little bit to the right (2 pixels to the right and down ).
Finally, set prevWin to visible and let JavaScript know that prevWin should be hidden when the mouse leaves the preview window.
How to Use AJAX for hyperlinks
<Script>
Function changeA ()
{
Var r = document. getElementById ("Arch ");
If (r. innerText = "click") r. innerText = "changed"; else r. innerText = "click ";
}
</Script>
<A href = "javascript: void (0)" onclick = "changeA ()" id = "Arch"> click </a>
See
How does ajax load pages and click the Page Link without redirecting?
The Edit button is set to open in a new window.
You can just open it in the current window.
Remove the target = "_ blank" marked by.