Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,I'm trying to write code in vs 2010 ,so whenever I go to one of the links on my page by the mouse ,it can show me a div tag ,which it's content is loaded by ajax call from other html files (these html files contain only p tags,but It doesn't work ,even I try to track my code by firebug ,but it seems that there is no request by ajax call ,or can't load my .js file

HTML
<link rel="Stylesheet" type="text/css" href="Styles/MyStyle.css" />
       <script type="text/javascript" src="Previewlinks.js"></script>
    <ul>
        <li><a href="Page1.htm">Page1</a></li>
        <li><a href="Page2.htm">Page2</a></li>
        <li><a href="Page3.htm">Page3</a></li>
        <li><a href="Page4.htm">Page4</a></li>
     </ul>
    <div id="mydiv">
    </div>

#mydiv
{
    background-color:Gray;
    width:150px;
    height:200px;
    font-size:1em;
    font-family:Arial;
    padding:5px;
    position:absolute;
    visibility:hidden;
    border:1px solid Black;
    overflow:hidden;

    }

C#
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 = MakeRequest;
    }
}
function MakeRequest(evt) {
    if (evt) {
        var url = evt.target;
    }
    else {
        evt = window.event;
        var url = evt.srcElement;
    }
    xPos = evt.clientX;
    yPos = 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 { document.write('sorry,but an error occur in Ajax request'); }
}
function ShowContents() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var outMsg = xhr.responseText;
        } else {
            var outMsg = 'There was a problem' + xhr.status;
        }
        var prevwin = document.getElementById('mydiv');
        prevwin.innerHTML = outMsg;
        prevwin.style.top = parseInt(xPos) + 2 + 'px';
        prevwin.style.left = parseInt(ypos) + 2 + 'px';
        prevwin.style.visibility = 'visible';
        prevwin. önmouseout = function () {
            document.getElementById('mydiv').style.visibility = 'hidden';
        }
    }
}
Posted
Updated 1-May-13 2:19am
v5

Where is the PreviewLinks.js file located? In the same location as your aspx page? If not you will have to specify the complete relative path like:

<script type="text/javascript" src="../Scripts/PreviewLinks.js" />


If you look in to the script errors section of your browser's developer tools, you will find 404 errors. This indicates that the web application cannot locate your .js file.

Even if it's not due to the fact that the .js file is not found, the scripts window will give you more information on javascript errors. Post it here for someone to help...

Hope this helps!
 
Share this answer
 
Function MakeRequest(evt) have an argument evt and you are not passing any any thing in following line

allLinks[i].onmouseover = MakeRequest;
I think there is a problem in that.hope it helps you

Thanks
 
Share this answer
 
I think is 'length' instead of 'Length' = for (var i = 0; i < allLinks.Length; i++) {

could you try to make the DOM ready first, if not then when you assigned var xPos, yPos and call on init become not reference.

if you`re okay I give some other but using jquery :

C#
$(function () {

$("a").hover(function (event) {
        alert(event.target.href);
//event.target.href -> to get href that selector in order for you to make ajax request
//you can put ajax call here and can assign to that selected div
    });
});



So it only call ajax when the user hover to that element instead we initialize to all ajax call during page load.

Hope it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900