5

I found a website that has the same header for all the pages which never reloads when navigating through pages. After header loads once, it stays in place and the rest of the layout (content and footer) loads from page to page. Its pretty much like when you have a frameset but its not. Mouse hover the upper menu and click on any item and you will see what I mean.

What is this technique called? I would like to know the name so I can research about it and learn it.

Thank you.

Rick
  • 373
  • 5
  • 19
Cain Nuke
  • 2,843
  • 5
  • 42
  • 65

6 Answers6

9

After reviewing the website you submitted, I was able to find a JavaScript file, which as many people suggested uses Ajax to load the contents into the page without reloading it, after the webpage is loaded, the script triggers a hash change, which changes the URL to match the one you clicked on using window.history.pushState() as suggested by @escapedcat, the JavaScript also handles the animation and changes the classes of certain elements in the webpage to reflect its state (updating, etc.).

The code is uglified, but you can still see how this is done: link

Edit:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page 1</title>
</head>
<body>
    <header>
        <nav><a href="page.html">Link</a></nav>
    </header>
    <div id="content">
        this is the default content
    </div>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript">
    $('nav a').on('click', function(e){
        e.preventDefault();
        console.log('test');
    
        $.get(this.href, function(data){
            $('#content').html(data);
            console.log(this);
            history.pushState({foo:'bar'}, 'second page', 'page.html');
        });

    });
    </script>
</body>
</html>

page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page 2</title>
</head>
<body>
    This is page 2
</body>
</html>

The above changes the URL, you still have to do further development of the code to get full functionality, i.e. a back button.

You can refer to Page not reloading via 'back' when using pushState() / onpopstate to add this kind of functionality.

Edit:

The pushState() method:

As the documentation states, the pushState() method receives 3 arguments:

  1. a state object containing serialized information associated with the new history entry,

  2. the page title, and

  3. the page URL.

Each time a user navigates to a new history entry, the popstate method is fired and the state changes to represent the current state.

Robert Bradley
  • 548
  • 2
  • 21
javiercf
  • 811
  • 7
  • 16
  • I tried this code but its not working. Nothing happens as I click the link. – Cain Nuke Jan 21 '16 at 11:26
  • Edit: now it works but I had to move the scripts into head instead. Is that alright? I have some questions: what is the foo:'bar'} part for? Also, the seems to change automatically too for page2 which is great so I wonder if you can specify which bits of page2.html to load and which ones not? – Cain Nuke Jan 21 '16 at 11:35
  • @CainNuke if you don't know where to place scripts i strongly suggest to learn the basics first. – Sven van den Boogaart Jan 28 '16 at 13:43
4

The technique is called a "Single page application" (a.k.a. SPA). You can create a SPA using any framework or library you wish (e.g. AngularJS, ReactJS, jQuery, etc) as long as you follow some guidelines.

A single-page application (SPA) is a web application or web site that fits on a single web page with the goal of providing a more fluent user experience similar to a desktop application. In a SPA, either all necessary code – HTML, JavaScript, and CSS – is retrieved with a single page load, or the appropriate resources are dynamically loaded and added to the page as necessary, usually in response to user actions.

Assuming that we're not loading all of the content in advance, the basic guidelines are:

  1. When the server loads the page for the first time, you decipher the URL and return the appropriate page. The page should contain an identical template for all pages, while only a dedicated part of the html file is reserved for the dynamic content.
    • For example, all pages in my website should return the same header and load the same javascript files, the part that changes is only my #content element which contains the html of that specific page, according to the URL.
  2. When the user clicks an internal link you perform the following operations:
    • Prevent the link from navigating, using event.preventDefault()
    • Change the URL yourself, using the history API (history.pushState(stateObj, title, path))
    • Load the content using Ajax and replace the the existing content with the new one.
  3. Listen to popState events to detect change in the URL due to the use of the back and forward buttons. (window.addEventListener('popstate', handler))
    • Load content according to the new URL.

Note: The server needs to provide the content (without the rest of the template) using Ajax.

Concerning the "How", that is completely up to you as long as you understand the workflow of a SPA.

iMoses
  • 4,338
  • 1
  • 24
  • 39
1

The technology used is Angular JS. If you want to learn this technology you can use http://www.w3schools.com/angular/ or http://www.tutorialspoint.com/angularjs/

.If you want to see the page souce code you can right click and then go to inspect and right click and then go to inspect view page source. By Inspecting you will see the real time changes that are happening in the backend.

Rick
  • 373
  • 5
  • 19
  • The web page is built using PHP. – Rick Jan 15 '16 at 07:20
  • Thanks, I will have a look at it. I know the page is in php but this technology can be used with html pages too? I tried downloading the site to my computer but the menu wont even show here. – Cain Nuke Jan 15 '16 at 08:30
  • yes you can implement it on HTML pages . The reason its not working is because the JQuerry file must be external.What he has done is JQuery (Hide and show methods) http://www.w3schools.com/jquery/jquery_hide_show.asp You can see the above site to know more. Hope you have got your answer :-) – Rick Jan 15 '16 at 11:23
  • The URL change is done via [pushState](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState). So it looks like it's loading a different page, but it's actually not. It is just manipulating the URL with JavaScript. – escapedcat Jan 18 '16 at 05:06
  • the site does use php that is why when you download it it doesn't show you the underlying code but only the rendered html as @escapedcat says the javascript manipulates the url using `pushState` however the site does not use angular, but angular views can do this in a much simpler way, you can combine php and angular to get dynamic data in there as well – javiercf Jan 18 '16 at 17:28
  • @javiercf I guess its using angular – Rick Jan 21 '16 at 15:08
  • @RickFernandes this one in particular does not, I do however think using angular would make it easier, but the current structure is better for SEO, as all the pages exist and can be read by robots but the JS makes for a better user experience – javiercf Jan 21 '16 at 17:47
1

Im not sure how this person does it but how you could make this is: When an user clicks on a menu item load the content with ajax (no refresh)

$(document).ready(function(){
    $("menu-item").click(function(){
          $("#content").load("something");
    });
 });

You can update the url with :

window.history.replaceState(“object or string”, “Title”, “/another-new-url”);

this updates the url without a refresh

If a user lands on a specific url you can pass the required data based on the url to load the right content.

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • This is a very interesting solution. Simple and practical. I guess that instead of "something" I must place the name of the content? – Cain Nuke Jan 19 '16 at 06:12
  • @CainNuke yes something will be the response of the ajax call given from you backend – Sven van den Boogaart Jan 19 '16 at 19:23
  • what does object or string mean? I have this: { 'page_id': 1, 'user_id': 5 }; but I dont quite understand what is it for. – Cain Nuke Jan 20 '16 at 16:07
  • @CainNuke history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one. Note that this doesn't prevent the creation of a new entry in the global browser history. replaceState() is particularly useful when you want to update the state object or URL of the current history entry in response to some user action. https://developer.mozilla.org/en-US/docs/Web/API/History_API – Sven van den Boogaart Jan 20 '16 at 17:10
  • I tried but I have a problem. When I click on the button the new content is loaded and the URL updated. However, if I go back in my browser only the ulr changes back but the content doesnt. Is it normal or I need something else? – Cain Nuke Jan 20 '16 at 17:27
  • @CainNuke can you post some of the code you have up until now? – javiercf Jan 20 '16 at 17:44
  • @CainNuke edited my answer to provide you with details of how to do this, bear in mind ajax requests don't work if you are working out of a directory, you need to put this on a local webserver to test it because `Cross origin requests are only supported for protocol schemes` – javiercf Jan 20 '16 at 18:36
  • @CainNuke what was wrong with this answer? I will remove or edited based on your comment, I saw you accepted an other answer thats almost the same but posted 5 days after this one. – Sven van den Boogaart Jan 28 '16 at 13:45
1

You can use Meteor.js too.

Their own website is built using meteor.js and you can clearly see the functionality you want on that website.

code
  • 2,115
  • 1
  • 22
  • 46
0

You can achieve same by the a layers in css (click here to know more)

For the site you suggested there will be three layers

  1. Image

  2. Header(which will scroll down on mouse hover)

  3. The Background image (That is Hidden by the Header)
Rick
  • 373
  • 5
  • 19
  • But when you go to another page, the header will reload, wont it? – Cain Nuke Jan 15 '16 at 13:20
  • No It wont if you use mouse hover function on layer 2 because the you are just replacing layer 1 and layer 3 via JQuerry. – Rick Jan 15 '16 at 15:11
  • Uh... The header will still reload. There's just the illusion that it hasn't. This won't work if you're not hovering over the element either. – Lisa Apr 14 '20 at 14:40