开发者

AJAX Results in Div Dissapearing

Using AJAX to call a CGI script which opens a file and prints it out in a DIV.

This works fine on a test page I have but for some reason in another page it will print the contents into the DIV and then will clear out the DIV. Using alert() I can see the readystate changing from 1,2,3,4,1,4 and then after it his 4 a second time the div contents clear out. It actually calls the updatepage() function twice but on the second time the DIV clears out. When I check in firebug the innerHTML changes from having the content to having "".

In my test page this does not happen even though the same sequence of readystate occurs.

Here is the snippet of code where I am having problems:

s开发者_开发知识库elf.xmlHttpReq.open('POST', cgiScript, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function()
 {
  if (self.xmlHttpReq.readyState == 4)
   {
    updatepage(self.xmlHttpReq.responseText);
   }
 }


function updatepage(str) 
   {
    document.getElementById("result").innerHTML = str;
   }

EDIT !!!!!!!!!!!!!!!!!

Ok I think I found out what is wrong but I'm not sure why.

In the anchor html href="" was causing the problem. Once I removed it the div stayed populated.

This would cause the DIV to dissapear after printing

        <div class="menulinkboxes" id="listdiv">
        <a class="mainmenulinks"
        onclick='openFile("list,autos")'
        href="">auto-list</a>
        </div>

Replacing href="" with href="#" fixed it. # means this page

        <div class="menulinkboxes" id="listdiv">
        <a class="mainmenulinks"
        onclick='openFile("list,autos")'
        >auto-list</a>
        </div>


I know you answered this, but just thought I expand your answer quick.

href="#"

The "#" tells the browser to look for an anchor tag, so if you leave the # sign empty after, it anchors to itself.

Also, I've found that innerHTML can cause problems, and the DOM methods are really handy. For retrieving that value it should work fine however. Just wanted to mention it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜