10

I'm trying to create a website, and I'm trying to figure out how to load a page.

For example:

You click on the navigator "Home" then a the bottom of the screen It loads a page witch text saying for example "Hello Word!".

Does anybody know what to do? I'm pretty sure It involves JavaScript.

5
  • w3.org/wiki/HTML_links_-_lets_build_a_web#What_are_links.3F Commented Sep 21, 2013 at 8:19
  • Not very helpful of my understanding, sorry. Commented Sep 21, 2013 at 8:29
  • Are you talking about single page applications? You always stay on the same page, but the content is reloaded. Commented Sep 21, 2013 at 8:41
  • Just like how this website works - habbosecrets.com. When you click "News" It loads text's and images. Commented Sep 21, 2013 at 8:43
  • 2
    I actually think this is a useful question + answer - so many answers on a similar topic jump straight in with a framework Commented Jun 13, 2014 at 10:27

3 Answers 3

22

To dynamically load content, you could make an AJAX call using XMLHttpRequest().

In this example a url is passed to the loadPage() function, in which the loaded content is returned.

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="text/javascript">
            function loadPage(href)
            {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open("GET", href, false);
                xmlhttp.send();
                return xmlhttp.responseText;
            }
        </script>
    </head>

    <body>
        <div onClick="document.getElementById('bottom').innerHTML = 
                      loadPage('hello-world.html');">Home</div>

        <div id="bottom"></div>
    </body>

</html>

When the div element containing text of "Home" is clicked, it sets the html of div element with id of "bottom" to content found in the "hello-world.html" document at the same relative location.

hello-world.html

<p>hello, world</p>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but a question in mind. What about more then 1 pages. What would happen?
Multiple documents may be loaded, replacing or appending content by repeatedly calling this implementation.
@MuhammadUmer Above demonstrates a synchronous request.
nvm i just rerealized that last parameter which is false defines whether request is going to be async or not. :D
Syntax: open(method,url,async)
|
0

Ok, what you are looking for is a single page application.

There are plenty of technologies implementing it, but not that easy.

Here is a tutorial I followed for doing it : http://andru.co/building-a-simple-single-page-application-using-angularjs

If you want to go further with SPAs, you will certainly need angular templates.

Comments

-1

It's not necessary to use Ajax calls to load html. Use them when need the server to process data successfully before loading the page, if processing was successful.

If you don't have this concern, and you just want to load the html without data processing, a simple anchor tag will suffice:

 <a href="path/to/hello.html">click</a>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.