Click here to Skip to main content
13,148,427 members (60,725 online)
Rate this:
Please Sign up or sign in to vote.
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>
        <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>
    <div id="mydiv">
    border:1px solid Black;

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 =;
    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;'GET', url, true);
    } 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; = parseInt(xPos) + 2 + 'px'; = parseInt(ypos) + 2 + 'px'; = 'visible';
        prevwin. önmouseout = function () {
            document.getElementById('mydiv').style.visibility = 'hidden';
Posted 1-May-13 2:06am
Updated 1-May-13 2:19am
Rate this: bad
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!
Rate this: bad
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

Rate this: bad
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) {
// -> 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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170924.2 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2017
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