Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1. #1
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Post [Tutorial] -> Ajax "page loader" to div element

    Well I've seen a few people wanting to make cutenews load stuff into the same div, sort of like replacing an iframe, so I thought I'd post some code to help you along that.

    [Heres an example]

    Basically to load a page into a div element basically use this javascript function

    Code:
    loadPage ( 'page.html', 'div' );
    I'll explain the attributes:

    Green -> The page you wish to load
    Red -> The div element you wish to load it into, eg:

    Code:
    <div id="div"></div>
    Basically that will load the contents of "page.html" into the element id "div"

    Heres the javascript:

    Code:
        <script type="text/javascript">
            var div;
            function loadPage ( page, obj )
            {
                div = document.getElementById( 'content' );            
                if ( window.XMLHttpRequest )
                {
                   httpRequest = new XMLHttpRequest();
                }
                else if ( window.ActiveXObject )
                {
                   httpRequest = new ActiveXObject ( 'Microsoft.XMLHTTP' );
                }
                httpRequest.open ( 'GET', page, true );
                httpRequest.onreadystatechange = processPage;
                httpRequest.send ( false );
            }    
            function processPage ()
            {
                if ( httpRequest.readyState == 4 )
                {    
                    if ( httpRequest.status == 200 )
                    {
                        if ( httpRequest.responseText == '' )
                        {
                            div.innerHTML = 'Failed to load page';
                        }
                        else if ( httpRequest.responseText != '' )
                        {
                            div.innerHTML = httpRequest.responseText;
                        }
                    }
                }
            }
            
        </script>
    and heres a test page:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    
        <title>Test page</title>
    
        <script type="text/javascript">
            var div;
            function loadPage ( page, obj )
            {
                div = document.getElementById( 'content' );            
                if ( window.XMLHttpRequest )
                {
                   httpRequest = new XMLHttpRequest();
                }
                else if ( window.ActiveXObject )
                {
                   httpRequest = new ActiveXObject ( 'Microsoft.XMLHTTP' );
                }
                httpRequest.open ( 'GET', page, true );
                httpRequest.onreadystatechange = processPage;
                httpRequest.send ( false );
            }    
            function processPage ()
            {
                if ( httpRequest.readyState == 4 )
                {    
                    if ( httpRequest.status == 200 )
                    {
                        if ( httpRequest.responseText == '' )
                        {
                            div.innerHTML = 'Failed to load page';
                        }
                        else if ( httpRequest.responseText != '' )
                        {
                            div.innerHTML = httpRequest.responseText;
                        }
                    }
                }
            }
            
        </script>
    
    </head>
    
    <body>
    <a href="javascript:loadPage( 'test_page.html', 'content' )">Load</a>
    <div id="content"></div>
    
    </body>
    </html>
    Any questions, post below.
    Hi, names James. I am a web developer.

  2. #2
    Join Date
    Sep 2005
    Location
    East London
    Posts
    3,028
    Tokens
    0

    Latest Awards:

    Default

    Nice job :p

  3. #3
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Thank you, any problems just post below.
    Hi, names James. I am a web developer.

  4. #4
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    Could I do something like <a href="lol.php" onClick="(loadPage ( 'page.html', 'div' )">Link</a> or something like that?

  5. #5
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Yes you can but onclick="loadPage ( 'page.html', 'div' );"
    Hi, names James. I am a web developer.

  6. #6
    Join Date
    Mar 2008
    Posts
    927
    Tokens
    0

    Default

    Good job!!

  7. #7
    Join Date
    Feb 2006
    Location
    Ontario Canada
    Posts
    4,587
    Tokens
    0

    Latest Awards:

    Default

    .. Dynamic Drive has all this on there website.
    http://www.dynamicdrive.com/dynamici...jaxcontent.htm

    .:.:#14:.:. .:.: Impossible Is Nothing :.:. .:.: 845 Rep:.:.
    .:.: Stand up for what is right, even if you stand alone:.:.


  8. #8
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Well done, though this is much easier to understand than theirs. If you wish to use theirs go ahead I'm not stopping you. Though I did ask for questions to be posted only so there was no need in your off-topic post.
    Last edited by Protege; 07-07-2008 at 06:25 PM.
    Hi, names James. I am a web developer.

  9. #9
    Join Date
    May 2006
    Location
    Hull
    Posts
    7,701
    Tokens
    2,430
    Habbo
    Moh

    Latest Awards:

    Default

    I tried adding a back button but I failed

    Any idea how to do a back button?

  10. #10
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    A back button, explain more
    Hi, names James. I am a web developer.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •