Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Ajax VS2010 DOM
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
 
<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;
 
    }
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 1-May-13 3:06am
Edited 1-May-13 3:19am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 :
 
$(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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 219
4 Mathew Soji 160
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100