Some ajax help

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Some ajax help

Post by jefffan24 »

Is there a way to load a php script with ajax on page load. I've gotten this script I have for onclick to do it onload but it works in everything but in IE (stupid piece of junk)... What happens is it loads when you go to the page, but if you login it stops working. Not much changes, no div changes, so I'm not quite sure what is happening.

If you want to test it:

http://jefffan24.com/copy/

Log In:

Username: testuser
Password: freckles

Code: Select all

 
    function ajaxFunction(){
        var ajaxRequest; //The var
        
        try {
            //Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e) {
            //IE browsers
            try {
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e){
                try {
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    //Something went wrong
                    alert("Your Browser be broke!");
                    return false;
                }
            }
        }           
    
    
        // Creates a function that will receive data sent from the server
        
        ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
                var ajaxDisplay = document.getElementById('news');
                ajaxDisplay.innerHTML = ajaxRequest.responseText;
            }
        }
        ajaxRequest.open("GET", "show_news.php", true);
        ajaxRequest.send(null);
    }
 
So I guess my question is how do i get this to work in IE.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Some ajax help

Post by kaszu »

ajaxFunction works correctly, possible problem is that you are overwriting onload

Code: Select all

window.onload = divHide;
instead you should add it to onload

Code: Select all

window.onload = function () { divHide(); ajaxFunction(); };
but then remove <body onload="ajaxFunction()"> or ajaxFunction will be called twice in non-IE browsers


Edit: Sorry, I was wrong. You have <div> before DocType declaration, that's why <body onload=""> event is not called.

Code: Select all

<div name="logout" style="position:relative; left:10px;"><a href="logout.php">Logout</a><br /><a href="report.php">Report A Bug</a></div>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Some ajax help

Post by jefffan24 »

That worked perfectly, had no clue that would mess with javascript. Thanks :D

Do you still think I should put both functions in the window.onload ?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Some ajax help

Post by kaszu »

I would.
Post Reply