?
Solved

Ajax content appears in HTML DIV then disapear right away for no reason?

Posted on 2009-07-01
4
Medium Priority
?
304 Views
Last Modified: 2013-11-18
Hello,

I have a simple Ajax script that retrieves the content of a file via URL and displays it in a <div> element. Nothing fancy except for the fact that the content retrieved appears at first and then magically disappear right after?

Since this is a rather simple example here is all the code, I am left wondering after surfing probably too many web sites and PDF documents.

Some help would be welcome, thanks!
CODE FOR HTML PAGE:
 
<html>
    <head>
        <script type="text/javascript" language="JavaScript" src="scripts/bridge_locale.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test</title>
    </head>
    <body>
        <form>
            <input type="submit" value="pull data" onclick="callAHAH('http://localhost/test.txt', 'displaydiv', 'blablabla...');" />
        </form>
        <div id="displaydiv"></div>
    </body>
</html>
 
 
CODE FOR JAVASCRIPT FILE:
 
function callAHAH(url, pageElement, callMessage) {
    //document.getElementById(pageElement).innerHTML = callMessage;
    try {
        req = new XMLHttpRequest(); //FF
    } catch (e) {
        //alert("XMLHttpRequest did not instantiate");
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP"); //IE most common
        } catch(e) {
            //alert("Msxml2.XMLHTTP did not instantiate");
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP"); //IE less common
            } catch (e) {
                //alert("Microsoft.XMLHTTP did not instantiate");
                req = false;
                //alert("Error: could not instantiate required control in bridge script");
            }
        }
    }
 
    req.onreadystatechange = function() { responseAHAH(pageElement); };
    req.open("GET", url, true);
    req.send(null);
}
 
function responseAHAH(pageElement) {
    alert("req.readyState="+req.readyState);
    alert("req.status="+req.status);
    var output = '';
    if (req.readyState == 4) {
        if (req.status == 200) {
            output = req.responseText;
            document.getElementById(pageElement).appendChild(document.createTextNode(output));
            alert("wait a minute before disapearing...???!!!");
        }
    }
}

Open in new window

0
Comment
Question by:deve-lopez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 1000 total points
ID: 24759933
How many alert( "req.readyState=4") and alert('req.status=200") messages do you get?
0
 

Accepted Solution

by:
deve-lopez earned 0 total points
ID: 24760190
I actually found the solution... it was right under my eyes :-)
In the html code I changed the type of the input control from "submit" to "button":

<input type="submit" value="pull data" onclick="callAHAH...
INTO
<input type="button" value="pull data" onclick="callAHAH...

Now it works the data remain on the page and do not disappear anymore.
Taking a break away from the computer was the actual solution :-)
0
 
LVL 5

Expert Comment

by:Opalelement
ID: 24760934
When you click submit, it tries sending the form data to the server. It wasn't that the content was disappearing, it was that you were essentially "refreshing" the page each time;)
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24785694
Thanks for the assist and the point.

Thank you more so for providing your solution.

Good luck & have a great day.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question