Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 3 123 LastLast
Results 1 to 10 of 29
  1. #1
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default [Script] Ajax Page Load

    Ok, theres been a few people asking for this lately so i thought id provide a script ive written. What an ajax load normaly is, is a method where an xmlhttp request is made via javascript and the content of a page is dynamically loaded in to a div of your choice.
    Currently im going to provide a very basic script, which just takes a page and displays the contents in a div, via ajax.
    I may later add an advanced version which will support additional function, to work with a php navigation meaning search engines can still crawl the content, the back button and favorites still works.

    Basic script

    Features:
    > Shows page is loading
    > dynamically loads page to a div.

    Code:
    <script>
    //Set up variables
    var xmlhttp ;
    var thediv;
    function LoadPage(page,usediv) {
             // Set up request varible
             try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}
             //Show page is loading
             document.getElementById(usediv).innerHTML = 'Loading Page...';
             //scroll to top
             scroll(0,0);
             //Store div in use
             thediv = usediv;
             //send data
             xmlhttp.onreadystatechange = ShowPage;
             xmlhttp.open("GET", page);
             xmlhttp.send(null);
             //Stop any link loading normaly
             return false;
    }
    function ShowPage(){
             //Check page is completed and there were no problems.
             if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                    //Write data returned to page
                    document.getElementById(thediv).innerHTML = xmlhttp.responseText;
             }
    }
    </script>
    This function can now be called via creating a link like this.

    <a href="mypage.htm" onclick="return LoadPage('mypage.htm','mydiv');">

    The above would place the contents of "mypage.htm" in to a div with the id "mydiv" such as could be written '<div id="mydiv"></div>'

    The parts of the link in bold being what need editing.

    I wrote the script on the go so its completely untested but should hopefully work. If it doesnt post back with the problem and ill sort it out for you.

    Edit: phew noticed just in time, id forgotten to store the div id in the function independents variable, which would have stopped it from working, should be fixed now.
    Last edited by Mentor; 17-04-2007 at 03:30 PM.

  2. #2
    Join Date
    May 2005
    Location
    /etc/passwd
    Posts
    19,110
    Tokens
    1,139

    Latest Awards:

    Default

    Very Usefull and very kind +REP if i can
    Quote Originally Posted by Chippiewill View Post
    e-rebel forum moderator
    :8

  3. #3
    Join Date
    Jul 2005
    Location
    North Wales
    Posts
    4,233
    Tokens
    1,544

    Latest Awards:

    Default

    Great now watch all the habbo site change from iframes to this, thats why no one was giving it out, but it's your code you can do what you want with it, + rep anyway.

  4. #4
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    Quote Originally Posted by redtom View Post
    Great now watch all the habbo site change from iframes to this, thats why no one was giving it out, but it's your code you can do what you want with it, + rep anyway.
    Whats wrong with them doing that o.0 The scripts simplistic anyway, its not even close to being functionally equivalent to a proper ajax loading system (such as i wrote for thybag) it cant really do anything above or beyond what an Iframe can, it just looks a bit better.
    Last edited by Mentor; 17-04-2007 at 04:02 PM.

  5. #5
    Join Date
    Jan 2007
    Posts
    651
    Tokens
    0

    Default

    REP+ I was going to post the tutorial written on DynamicDrive, but someone was saying somthing about fnasites will be ripping it.So i left it, nice one though
    James


    Connected to reality through a proxy server.

  6. #6
    Join Date
    Nov 2006
    Location
    Leeds, Yorkshire
    Posts
    992
    Tokens
    0

    Default

    Nice thanks +rep
    can I ask you, how do you do that "not using firefox" thing on your site?
    Thanks


  7. #7
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Why not just use a simple preg_match function checking for say MSIE against $_SERVER['HTTP_USER_AGENT'] for internet explorer?

  8. #8
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:


  9. #9
    Join Date
    Dec 2006
    Location
    None of your business!
    Posts
    2,492
    Tokens
    50

    Latest Awards:

    Default

    Quote Originally Posted by 01101101entor View Post
    Ok, theres been a few people asking for this lately so i thought id provide a script ive written. What an ajax load normaly is, is a method where an xmlhttp request is made via javascript and the content of a page is dynamically loaded in to a div of your choice.
    Currently im going to provide a very basic script, which just takes a page and displays the contents in a div, via ajax.
    I may later add an advanced version which will support additional function, to work with a php navigation meaning search engines can still crawl the content, the back button and favorites still works.

    Basic script

    Features:
    > Shows page is loading
    > dynamically loads page to a div.

    Code:
    <script>
    //Set up variables
    var xmlhttp ;
    var thediv;
    function LoadPage(page,usediv) {
             // Set up request varible
             try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}
             //Show page is loading
             document.getElementById(usediv).innerHTML = 'Loading Page...';
             //scroll to top
             scroll(0,0);
             //Store div in use
             thediv = usediv;
             //send data
             xmlhttp.onreadystatechange = ShowPage;
             xmlhttp.open("GET", page);
             xmlhttp.send(null);
             //Stop any link loading normaly
             return false;
    }
    function ShowPage(){
             //Check page is completed and there were no problems.
             if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                    //Write data returned to page
                    document.getElementById(thediv).innerHTML = xmlhttp.responseText;
             }
    }
    </script>
    This function can now be called via creating a link like this.

    <a href="mypage.htm" onclick="return LoadPage('mypage.htm','mydiv');">

    The above would place the contents of "mypage.htm" in to a div with the id "mydiv" such as could be written '<div id="mydiv"></div>'

    The parts of the link in bold being what need editing.

    I wrote the script on the go so its completely untested but should hopefully work. If it doesnt post back with the problem and ill sort it out for you.

    Edit: phew noticed just in time, id forgotten to store the div id in the function independents variable, which would have stopped it from working, should be fixed now.
    Thanks for that Carl, this could come in useful at some point. + rep. Well done.

  10. #10
    ScottDiamond Guest

    Default

    Why did you just quote the whole post? And Carl, thanks for explaining every line to me in MSN lol.

Page 1 of 3 123 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
  •